Introduction
In JavaScript, variables are used to store data that can be used later in the program. The var keyword was traditionally used to declare variables in older versions of JavaScript. However, with the introduction of ES6 (also known as ECMAScript 2015), the let keyword was introduced to provide better control and flexibility when working with variables.

This article explains the differences between var and let in JavaScript, helping beginners understand their use cases and limitations.
the var Keyword in JavaScript
The `var` keyword is one of the oldest ways to declare variables in JavaScript. It has been around since the beginning of the language. When you declare a variable using `var`, it is function-scoped. This means the variable is accessible anywhere within the function where it is declared.
Characteristics of var
- Function Scope: Variables declared with var are scoped to the function in which they are declared or globally if declared outside a function.
- Hoisting: Variables declared with var are hoisted to the top of their scope, meaning they can be used before they are defined, though their value will be undefined until the declaration is encountered.
- Re-declaration: var allows re-declaring the same variable within the same scope, which can sometimes overwrite important data accidentally.
Function Scope
Function scope means that a variable declared with `var` is only available inside the function where it is defined. If you declare it outside any function, it becomes globally scoped & accessible throughout your code.
For example:
function example() {
var x = 10;
if (true) {
var x = 20; // This will overwrite the previous value of x
console.log(x);
}
console.log(x);
}
example();
Output
20
20
In this example, the variable `x` is declared using `var` inside the function. Even though it is redeclared inside the `if` block, it still refers to the same variable. This is because `var` does not have block scope.
Hoisting
Another important behavior of `var` is hoisting. Hoisting means that JavaScript moves all variable declarations to the top of their scope during the compilation phase. This allows you to use a variable before it is declared in the code.
For example:
console.log(y);
var y = 5;
Output
undefined
Here, the variable `y` is hoisted to the top of its scope, but its value is not initialized until the assignment line. That’s why `console.log(y)` outputs `undefined` instead of throwing an error.
Global Scope Issues
One downside of `var` is that it can accidentally create global variables. If you forget to declare a variable with `var` inside a function, JavaScript automatically treats it as a global variable. This can lead to unexpected behavior & bugs in your code.
For example:
function example() {
z = 30; // z becomes a global variable
}
example();
console.log(z);
Output
30
This behavior can cause issues in larger projects where variable names might conflict.
Example: Using var
function exampleVar() {
console.log(a);
var a = 10;
console.log(a);
if (true) {
var b = 20; // `b` is accessible outside the if block
console.log(b);
}
console.log(b);
}
exampleVar();
Output
undefined (hoisting)
10
20
20