Variables are the containers for storing values or expressions They are used to store data temporarily in the computer
section = "3U"
console.log(section)- Case sensitive
- Must start with an an alphabet or underscore
- Cannot start with a number
- Cannot use reserved words as variables
- Should be meaningful
- Cannot contain space or hyphen (-)
- score β
- _id β
- 01 π«
- await π«
There are three methods of declaring a variable in js
- let keyword
- const keyword
- var keyword
| Keyword | Access | Assignment | Initialise |
|---|---|---|---|
| let | {...} | Reassignable | Optional |
| const | {...} | Final | Required |
| var | function | Reassignable | Optional |
let count = 0
count = 1 //This works
const count = 0
count = 1 //This doesn't work
var count = 0
count = 1 //This works{
let blockVariable = "I am hidden";
var functionVariable = "I am visible";
}
console.log(blockVariable); //This doesn't work
console.log(functionVariable); //This works// Both works
let name="Rohan" , role="Student";
let name="Rohan";
let role="Student"; Note
- If you assign values to variables that have not yet been declared, the variables will automatically be declared.
x = 120; // No error here- "var" has some confusing behaviors regarding scope and hoisting, so modern developers generally avoid it in favor of let and const.
previous