Example: Think of a box named Age which stores the age of a person. So, the name is the variable name, and the age stored in it is the value.
Rules are as follows:
- We can use letters, digits, symbols like dollar sign ($) and underscore ( _ ) in a variable name.
- We cannot start a variable name with a digit (0-9).
- We can separate different variable declarations using a comma.
- Declaring two variables in a single line separated by comma:
- Using reserve keywords while declaring a variable:
- Valid variable name examples:
- Invalid variable name examples:
Declaring a Variable
In the mixed keyword example, we've declared variables x using var, y using let, and z using const. This illustrates how variables declared with different keywords behave with regard to reassignment and scope.
Initializing a Variable
After you have declared a variable, you can initialize that variable with a value. This initialization can be done by just writing the variable name followed by an equals sign and then the value you want the variable to store.
Now, try writing these lines in the console. After writing these lines, you can write only the variable names and press enter, the output will be the value stored in it.
Note: You can even declare and initialize a variable in a single line. Example:
When to Use var, let, or const?
Use const when you want to declare a variable that should not be reassigned. It's also block-scoped like let, but you cannot reassign values to variables declared with const. You should use const for values that should remain constant throughout their scope.
- The main difference between var and let is that the scope of the variable that is defined by let is limited to the block in which it is declared, whereas the variable declared using var has the global scope, i.e., can be used throughout the code.
- If we declare a variable using var outside of any block (i.e., in the global scope), then the variable gets added to the window object, whereas variables declared with let will never get added to it.
- We cannot declare the same variable multiple times if one of them is declared using let, whereas we can declare the same variable any number of times using var.
- Variable hoisting can be done using var, but hoisting cannot be done using let.
Try the below codes in your browser's console and you will understand it better.
In this case, the scope of the variables x and y is only limited to the function, that’s why we cannot find the reference to the variables x and y outside the function, thus resulting in the reference error. We will describe the Variable Scope in the later part of this article.
Updating a Variable
Once a variable is declared or initialized, we can change its value anytime and anywhere within its scope. It is similar to re-initializing the variable. We can update/change the value by just typing the variable name followed by an equals sign and then followed by the new value we want it to store.
- Numbers can be used for mathematical operations such as addition, subtraction, multiplication, and division.
- Strings are sequences of characters enclosed within single (''), double ("") or backticks (``) quotes.
- They are used to represent textual data, such as words, sentences, or any combination of characters.
- Strings can be manipulated, concatenated, and compared in various ways.
- Booleans are a data type with only two possible values: true or false.
- They are commonly used for making decisions in conditional statements or controlling the flow of a program.
- Arrays are ordered collections of data, where each element can be of any data type (including other arrays).
- They are defined using square brackets  and can be accessed by their index.
- Arrays provide a convenient way to store and manipulate multiple values in a single variable.
- Objects are used to represent complex and structured data. They are often used to model real-world entities or data structures.
- Objects can have methods, which are functions associated with them, and properties, which are variables associated with them.
Also, one can change the stored value to any type of data, anytime freely.
Try running these lines in console for better understanding.
These constants are similar to variables but have some properties which are different from variables, such as:
- The constant must be initialized when you declare it, otherwise, it will throw a " SyntaxError: Missing initializer in const declaration ".
- Once assigned a value to a constant, you can’t re-assign values afterward. That is, a value is fixed to a constant.
Note: Though we cannot change the value of the constant, but if the constant value is an object, then we can modify the object's properties.
You can even add, or update or remove properties from a constant object, because you are just changing the details of the object, not the reference. The constant will still point to the same object.
- Variables are containers that store values.
- Variables can be declared using var and let keywords.
- Variables can be updated or modified anytime and anywhere in its scope.
- Global scope
- Local scope