In JavaScript, you can declare variables using the var
, let
, or const
keywords. Here’s how you can declare variables with each of these keywords:
var
: This keyword was traditionally used for variable declaration in JavaScript, but it has some scoping quirks. Variables declared with var
are function-scoped or globally-scoped.
var myVariable = 10;
let
: Introduced in ES6 (ECMAScript 2015), let
allows block-scoping of variables. It is generally recommended to use let
instead of var
for most cases.
let myVariable = 10;
const
: Also introduced in ES6, const
is used to declare constants, which are variables that cannot be reassigned. Like let
, const
is block-scoped.
const myConstant = 10;
Note: variables declared with var
and let
can be reassigned, while variables declared with const
cannot be reassigned once they are assigned a value.
Additionally, it’s good practice to always use let
or const
unless you have a specific reason to use var
due to its scoping behavior.
Type | Redeclaration | Reassignment | Scope | Hoisting | Initialization |
---|---|---|---|---|---|
var | Allowed | Allowed | Function/Global | Yes | Optional |
let | Not allowed | Allowed | Block | No | Required |
const | Not allowed | Not allowed | Block | No | Required |
- Hoisting: Variables declared with
var
are hoisted to the top of their scope, meaning they can be used before they are declared. This behavior can sometimes lead to unexpected results. Variables declared withlet
andconst
are not hoisted and must be declared before they are used. - Initialization: Variables declared with
var
can be left uninitialized, meaning they will have a default value ofundefined
. Variables declared withlet
andconst
must be initialized with a value before they can be used.
Declare variable in JavaScript example
Simple example code.
// using 'var'
var age;
age = 25;
// using 'let'
let name = "John";
// using 'const'
const PI = 3.14159;
console.log(age); // Output: 25
console.log(name); // Output: John
console.log(PI); // Output: 3.14159
Output:
Here’s an example to illustrate the differences:
function example() {
var x = 10;
let y = 20;
const z = 30;
if (true) {
var x = 50; // This reassigns the existing 'x' variable
let y = 60; // This creates a new 'y' variable in the block scope
const z = 70; // This creates a new 'z' variable in the block scope
console.log(x); // Output: 50
console.log(y); // Output: 60
console.log(z); // Output: 70
}
console.log(x); // Output: 50
console.log(y); // Output: 20 (original value outside the block remains unchanged)
console.log(z); // Output: 30 (original value outside the block remains unchanged)
}
It’s important to note that the variable name follows certain rules:
- It must start with a letter, underscore (_), or dollar sign ($).
- It can contain letters, digits, underscores, or dollar signs.
- It is case-sensitive.
Comment if you have any doubts or suggestions on this JS variable topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version