JavaScript Variables
Variable can be defined as a container to hold data. A variable’s value during the script. Variables names must start with a letter of the alphabet or an underscore. Keywords to create variables is as shows below:
Variables are declared with a var statement:
- var pi = 3.1416, x, y, name = "Dr. Dave" ;
- Variables names must begin with a letter or underscore and Variable names are case-sensitive
- Variables are untyped (they can hold values of any type)
- The word var is optional (but it’s good style to use it)
Example
Output
Using let keyword in another example, a, b, and c, are variables is as follows:
Example
Output
Using const keyword in another example, a, b, and c, are variables is as follows:
If you want a general rule: always declare variables with const.
If you think the value of the variable can change, use let.
Example
Output