Variables in JavaScript are used to store and manage data. They act as containers that hold values that can be changed or kept constant throughout a program. Understanding how to declare and use variables properly is essential for writing clean and efficient JavaScript code.
A variable is a named storage for data. In JavaScript, variables can hold various types of values, including numbers, strings, objects, and more.
JavaScript provides three ways to declare variables:
var
(Old and Avoided)Used in older JavaScript versions.
Has function scope (accessible within the function it is declared in).
Allows re-declaration of the same variable.
Can be problematic due to hoisting.
var message = "Hello";
var message = "Hi"; // Allowed but not recommended
console.log(message); // Output: Hi
let
(Modern and Preferred)Introduced in ES6 (2015).
Has block scope (only accessible within the block {}
it is declared in).
Cannot be re-declared within the same scope.
let name = "Alice";
name = "Bob"; // Allowed
console.log(name); // Output: Bob
const
(For Constants)Also introduced in ES6.
Used for values that should not change.
Must be assigned a value during declaration.
const pi = 3.14;
// pi = 3.1415; // Error: Assignment to constant variable
console.log(pi);
JavaScript has specific rules for naming variables:
Must start with a letter, _
, or $
.
Cannot start with a number.
Cannot be a reserved keyword (e.g., let
, return
, class
).
Case-sensitive (userName
and username
are different).
let userAge = 25; // Valid
let _score = 100; // Valid
let $amount = 500; // Valid
// let 1stName = "John"; // Invalid (cannot start with a number)
Scope determines where a variable can be accessed.
A variable declared outside any function is global and can be accessed anywhere.
let siteName = "Codercrafter";
console.log(siteName); // Accessible globally
Variables declared with var
inside a function are limited to that function.
function greet() {
var message = "Hello";
console.log(message); // Accessible inside function
}
// console.log(message); // Error: message is not defined
Variables declared with let
or const
inside {}
cannot be accessed outside the block.
{
let localVar = "Inside block";
console.log(localVar); // Works
}
// console.log(localVar); // Error: Not accessible outside block
Hoisting is JavaScript’s behavior of moving variable declarations to the top of their scope before code execution.
console.log(greetMessage); // Output: undefined
var greetMessage = "Hello";
var
is hoisted, but its value remains undefined
until assigned.
With let
and const
, hoisting occurs, but accessing them before declaration throws an error:
console.log(name); // ReferenceError
let name = "Alice";
Use const
by default, only use let
when reassignment is needed.
Avoid var
due to scoping issues.
Use meaningful names for readability.
Follow camelCase naming convention (userName
, totalAmount
).
Understanding JavaScript variables is crucial for writing clean and efficient code. Use let
for values that change, const
for constants, and avoid var
whenever possible. Following best practices ensures better readability and maintainability.