A variable is a container used to store data in JavaScript. The stored value can be changed and reused throughout the program.
JavaScript provides three ways to declare variables: var, let, and const.
A variable stores information that can be used later in a JavaScript program.
Variables can store text, numbers, Boolean values, objects, arrays, and more.
JavaScript provides three keywords for declaring variables.
| Keyword | Description |
|---|---|
| var | Old method, function-scoped. |
| let | Modern and recommended for changing values. |
| const | Used for values that should not be reassigned. |
For most programs, use let and const.
A variable can be declared without assigning a value.
The default value of an uninitialized variable is undefined.
You can assign a value while declaring a variable or later.
The assignment operator (=) stores the value in the variable.
Most programmers declare and initialize variables in one statement.
Variables can store different types of values.
You can declare multiple variables in a single line.
This saves space and keeps related variables together.
The value of one variable can be copied into another variable.
Now both variables contain the value 100.
JavaScript allows extra spaces and line breaks when declaring variables.
Proper formatting makes code easier to read.
JavaScript follows certain rules for naming variables.
| Rule | Example |
|---|---|
| Variable names are case-sensitive. | name and Name are different. |
| Can contain letters, digits, _ and $. | user1, _name, $price |
| Cannot start with a digit. | ❌ 1name |
| Cannot use reserved keywords. | ❌ var, function, return |
JavaScript treats uppercase and lowercase letters differently.
| Variable 1 | Variable 2 | Same? |
|---|---|---|
| name | Name | No |
| msg | MSG | No |
| age | Age | No |
Always use consistent naming throughout your program.
JavaScript is a dynamically typed language.
A variable can store different types of values during program execution.
The same variable can hold numbers, strings, booleans, and other data types.
Use the const keyword for values that should not be reassigned.
Constant variables have special rules.
A const variable must have a value when it is declared.
A const object cannot be reassigned, but its properties can be modified.
The object itself remains the same, but its contents can change.
In JavaScript, variables can have either global scope or local scope.
Variables declared outside any function are called global variables.
They can be used anywhere in the JavaScript program.
Variables declared inside a function are called local variables.
They can only be accessed within that function.
Trying to access msg outside the function will cause an error.
A function can access both global and local variables.
The global variable can be accessed everywhere, but the local variable exists only inside the function.
JavaScript allows variables to be created without using let or var.
Such variables become global variables and may create unexpected problems.
Which keyword is recommended for declaring variables that can change?