JavaScript variables can be declared using the var, let, or const keywords depending on your scoping and mutability requirements.
JavaScript variable declaration syntax
Declare variables in JavaScript using the keywords var, let, or const.
Use the equal (=) operator to assign values to variables in JavaScript.
Basic syntax examples:
var x = 5;
let y = 10;
const z = 15;
The var Declaration
The var
keyword represents JavaScript's original method for declaring variables, dating back to the language's earliest versions.
Variables declared with var
exhibit function scope behavior, meaning they're accessible throughout the entire function where they're declared, regardless of block boundaries.
This permissive scoping often leads to unexpected behavior, particularly when variables are declared inside loops or conditional statements but remain accessible outside those blocks.
Additionally, var
allows both redeclaration and reassignment within its scope—you can declare the same variable name multiple times without error, with later declarations overwriting earlier ones.
The let Declaration
Introduced with ES6, let
provides a more controlled approach to variable declaration by implementing block scope rules.
Variables declared with let
remain confined to their specific block, preventing the scope leakage issues common with var
.
While let
variables can be updated with new values, they cannot be redeclared within the same scope, reducing naming conflicts and accidental overwrites. This keyword was specifically designed to address the problematic behaviors of var
, offering developers more predictable variable behavior that aligns with scoping rules found in most other programming languages.
The const Declaration
The const
keyword creates variables that maintain constant values after initialization, requiring that the variable be assigned a value at the moment of declaration.
Like let
, const
follows block scoping rules and prevents redeclaration within the same scope. However, const
goes further by prohibiting reassignment entirely — once a value is assigned, it cannot be changed.
It's important to note that for reference types like objects and arrays, const
prevents reassignment of the variable itself but still allows mutation of the object's properties or array's elements
Comparison Table: var vs let vs const
Feature | var | let | const |
---|---|---|---|
Scope | Function scoped | Block scoped | Block scoped |
Hoisting | Hoisted and initialized with undefined | Hoisted but not initialized (TDZ) | Hoisted but not initialized (TDZ) |
Redeclaration | Allowed | Not allowed in same scope | Not allowed in same scope |
Reassignment | Allowed | Allowed | Not allowed |
Initialization | Optional | Optional | Required at declaration |
Temporal Dead Zone | No | Yes | Yes |
Example
// var: redeclaration & reassignment allowed
var name = "Alice";
var name = "Bob"; // Redeclaration OK
name = "Charlie"; // Reassignment OK
console.log(name); // "Charlie"
// let: no redeclaration, reassignment allowed
let city = "Paris";
// let city = "London"; // SyntaxError: Identifier 'city' has already been declared
city = "London"; // Reassignment OK
console.log(city); // "London"
// const: no redeclaration, no reassignment
const pi = 3.14;
// const pi = 3.1416; // SyntaxError: Identifier 'pi' has already been declared
// pi = 3.1416; // TypeError: Assignment to constant variable
console.log(pi); // 3.14
Naming Conventions and Best Practices
Following consistent naming conventions transforms your code from merely functional to professionally crafted and easily maintainable. The most widely adopted approach uses camelCase for standard variables and functions, where the first letter remains lowercase and subsequent words begin with uppercase letters. For example, userName
, calculateTotalSalary
, and isUserLoggedIn
clearly communicate their purpose while maintaining visual consistency throughout your codebase.
Constants require special consideration—use SCREAMING_SNAKE_CASE for values that represent unchanging configuration or mathematical constants. Variables like const MAX_RETRY_ATTEMPTS = 3
or const SECONDS_IN_MINUTE = 60
immediately signal their immutable nature and special significance. Boolean variables benefit from descriptive prefixes such as is
, has
, or can
(e.g., isLoading
, hasPermission
, canEdit
) that make conditional logic more readable.