Javascript Variables
Variables are used to store data in a program. In JavaScript, you can declare variables using the var, let, or const keywords. Let's explore each of them.
Variables are like a container and as same suggests the value of the container can be modified (reassigned).
Constants are used for constant value, unlike variable they cannot be reassigned. (Note they can't be reassigned, doesn't mean they cannot change. More on this later)
var keyword
The var keyword is the oldest way to declare variables in JavaScript.
Example:
1var x = 10;
let keyword
The let keyword was introduced in ES6.
Example:
1let y = 5;
const keyword
The const keyword is used to declare constants, which cannot be reassigned once they are initialized.
1const z = 20;
Variable Naming Rules
- Variable names are case-sensitive.
- They can start with a letter, underscore (_), or a dollar sign ($).
- After the first character, you can use letters, digits, underscores, and dollar signs.