You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A variable is simply an identifier or container that holds a value. It can contain data of different types, such as boolean, string, number, object, and array.
15
+
A variable is simply an identifier or container that holds a value. It can contain data of different types, such as boolean, string, number, object, and array.
16
+
16
17
> 💡 A boolean variable holds either True or False, a string variable contains a sequence of characters enclosed in quotation marks, and an array is a collection of values of any data type.
From the image above, the cup represents an empty variable; the milk and coffee are data of different data types. When you put the data into the cup, the cup becomes a variable of data type, coffee or milk.
21
22
22
23
## How to create variables in JavaScript
23
-
To declare (create) variables in JavaScript, you need to use some built-in keywords such as var, let, and const. Let's talk briefly about them.
24
+
To declare (create) variables in JavaScript, you need to use some built-in keywords such as var, let, and const. Let's talk briefly about them.
25
+
24
26
### The var keyword
25
-
The var keyword is the oldest way of declaring variables in JavaScript, and it's not advisable to use it in your JavaScript programs because it is not block scope.
27
+
The var keyword is the oldest way of declaring variables in JavaScript, and it's not advisable to use it in your JavaScript programs because it is not block scope.
28
+
26
29
> A variable is block-scoped when it only exists within the curly braces where it was declared and cannot be accessed outside the curly braces.
27
-
Let’s take a look at some examples.
30
+
Let’s take a look at some examples.
31
+
28
32
```js
29
33
var ageInWords ="Eighteen"; //ageInWords holds the string value
30
34
var male =true; //male holds a boolean value
@@ -44,15 +48,17 @@ let age = 20; //sets a variable to 20
44
48
age =5; // re-assign age to 5
45
49
console.log(age); // prints 5 to the console
46
50
```
47
-
The code snippet above shows a variable created using the `let` keyword with an initial value of 20, then changed to 5.
51
+
The code snippet above shows a variable created using the `let` keyword with an initial value of 20, then changed to 5.
52
+
48
53
### The const keyword
49
54
The const keyword is used to declare variables that cannot be re-assigned to another value. It holds constant values.
50
55
```js
51
56
constage=20;
52
57
age =10;
53
58
console.log(age) //returns a TypeError: Assignment to constant variable. (Invalid)
54
59
```
55
-
> 💡 Don’t use the `var` keyword. Use `const` when you need to declare a constant and `let` when the variable can be re-assigned to another value.
60
+
> 💡 Don’t use the `var` keyword. Use `const` when you need to declare a constant and `let` when the variable can be re-assigned to another value.
61
+
56
62
### Naming variables in JavaScript
57
63
When naming variables in JavaScript, there are a few things you need to consider.
58
64
- Variables can only start with a letter, underscore, or dollar sign. Numbers and other symbols can come after the first position.
0 commit comments