Variables are containers used to store values that can be used and manipulated throughout a program. They play a fundamental role in programming by allowing us to hold, retrieve, and update data.
In JavaScript, variables are declared using three keywords: var
, let
, and const
. Each of these has different behaviors and rules regarding scope, reassignment, and hoisting.
You declare a variable by using one of the keywords var
, let
, or const
, followed by the variable name.
var
: The old way of declaring variables (ES5 and earlier).let
: A more modern way, introduced in ES6 (2015), with block-scoping.const
: Similar to let
but used for constants (values that don’t change).var x = 5; // Using var
let y = 10; // Using let
const z = 15; // Using const
myVariable
and myvariable
are different.firstName
, totalAmount
).let firstName;
let _age;
let $price;
let 2cool; // Cannot start with a number
let my-name; // Hyphen is not allowed, use underscore or camelCase
You assign a value to a variable using the assignment operator (=
).
let name = 'John'; // String
let age = 25; // Number
let isStudent = true; // Boolean
undefined
:
let job; // Declared but undefined
console.log(job); // Output: undefined
JavaScript is dynamically typed, which means you don’t have to specify the type of variable when declaring it. The type is automatically determined based on the value assigned.
string
: Text data (e.g., 'Hello'
, "John"
)number
: Numeric data (e.g., 10
, 3.14
)boolean
: Logical data (true or false)undefined
: A variable that has been declared but not yet assigned a valuenull
: A special value that represents “no value” or “empty”symbol
: Unique, immutable value introduced in ES6bigint
: For very large numbers beyond the number
type’s safe limitlet myString = "Hello World"; // String
let myNumber = 42; // Number
let isTrue = false; // Boolean
let notAssigned; // Undefined
let emptyValue = null; // Null
var
, let
, and const
: Key Differencesvar
(function-scoped):undefined
).var x = 5;
var x = 10; // No error, re-declaration is allowed
x = 20; // No error, value can be updated
let
(block-scoped):{ }
).var
.let y = 10;
// let y = 15; // Error: Cannot re-declare a 'let' variable in the same scope
y = 20; // Can be updated
const
(block-scoped):const z = 100;
// z = 200; // Error: Cannot reassign a const variable
var
vs. let/const
:var
are function-scoped:
function myFunction() {
var x = 10;
}
console.log(x); // Error: x is not defined outside the function
let
or const
are block-scoped:
{
let y = 10;
}
console.log(y); // Error: y is not defined outside the block
var
are hoisted but not initialized until the assignment is reached. Variables declared with let
or const
are not hoisted in the same way and will throw an error if accessed before declaration.var
hoisting:console.log(a); // Output: undefined (hoisted but not initialized)
var a = 10;
let
hoisting:console.log(b); // Error: Cannot access 'b' before initialization
let b = 10;
let
: Allows reassignment of the variable’s value.
let name = 'Alice';
name = 'Bob'; // Reassignment is allowed
const
: Prevents reassignment of the value.
const age = 25;
// age = 30; // Error: Cannot reassign a constant
const
, the reference to the value is constant, but the contents of objects or arrays can still be mutated.
const myArray = [1, 2, 3];
myArray.push(4); // Allowed, because the array itself isn't reassigned
console.log(myArray); // Output: [1, 2, 3, 4]
let
and const
instead of var
to avoid issues with scope and hoisting.const
by default if you don’t plan to reassign the variable.let
when you need to reassign a variable.var
is function-scoped, while let
and const
are block-scoped.let
allows reassignment, but const
does not.var
, let
, and const
.Mastering variables is an essential first step in learning JavaScript, as they are the foundation for managing data in any program.