A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-var/ below:

JavaScript Var Statement - GeeksforGeeks

JavaScript Var Statement

Last Updated : 23 Jul, 2025

The var keyword is used to declare variables in JavaScript. It has been part of the language since its inception. When a variable is declared using var, it is function-scoped or globally-scoped, depending on where it is declared.

Syntax

var variable = value;

It declares a variable using var, assigns it a value, and allows it to be re-declared or updated within its scope.

Features of var Keyword 1. Function Scope

Variables declared using var are function-scoped. This means they are accessible anywhere within the function they are declared, even before their declaration due to hoisting.

JavaScript
function testVar() {
    var x = 10;
    console.log(x); // Output: 10
}

console.log(x); // Error: x is not defined

Output

Var Function Scope 2. Global Scope

If var is used outside of any function, it creates a global variable, accessible anywhere in the script.

JavaScript
var globalVar = "I am global";
console.log(globalVar); // Output: I am global

Here, globalVar is accessible globally and can be used anywhere in the script.

3. Re-declaration of Variables

var allows you to re-declare variables within the same scope without throwing any errors, which can lead to unintended behavior.

JavaScript
var a = 5;
var a = 10;
console.log(a); // Output: 10
4. Hoisting

Variables declared with var are hoisted to the top of their scope, meaning the declaration part is moved to the top, but not the initialization. This can result in undefined behavior if not understood properly.

JavaScript
console.log(hoistedVar); // Output: undefined
var hoistedVar = "Hoisted!";

Since the declaration is hoisted but not initialized, it prints undefined before the variable is assigned its value.

5. No Block Scope

Unlike let and const, var does not have block scope. Variables declared with var inside a block (like an if or for loop) are accessible outside that block.

JavaScript
if (true) {
    var blockVar = "I am not block scoped";
}
console.log(blockVar); // Output: I am not block scoped

Output
I am not block scoped

Here, blockVar is accessible outside the if block because var is function-scoped, not block-scoped.

6. Global Object Property

In browsers, variables declared with var in the global scope become properties of the window object.

JavaScript
var globalVar = "Global";
console.log(module.exports.globalVar); // Output: Global
Global Object Property

This behavior can lead to conflicts if not carefully managed, as globalVar is both directly accessible and part of the global window object.

7. Performance Considerations

Though modern JavaScript engines optimize the use of var, let and const are typically preferred for better predictability and readability. Using var can sometimes lead to unnecessary recalculations in loops or complex code.

JavaScript
// Inefficient loop 

var a = new Array(10).fill(0);
console.time('Execution time')
for (var i = 0; i < a.length; i++) {
    console.log(i)
}
console.timeEnd('Execution time')



// Optimized loop 

var len = a.length;
console.time('Execution time')
for (var i = 0; i < len; i++) {
    console.log(i)
}
console.timeEnd('Execution time')

Output
0
1
2
3
4
5
6
7
8
9
Execution time: 3.378ms
0
1
2
3
4
5
6
7
8
9
Execution time: 0.918ms

In this example, storing arr.length in a variable improves performance, particularly for large arrays, by avoiding recalculation in each iteration.

8. Backward Compatibility

The var keyword is fully supported in all versions of JavaScript, making it essential for maintaining older codebases.

JavaScript
var oldBrowserSupport = "Works everywhere!";
console.log(oldBrowserSupport); // Output: Works everywhere!

When var is used at the global level, oldBrowserSupport becomes a property of the window object in browsers, making it accessible globally.

9. var used with setTimeout()

When used inside loops with setTimeout(), var can cause unexpected behavior due to its function scope. The value of the variable will be shared across all iterations of the loop, leading to the same result for each setTimeout() call.

JavaScript
for(var i=0;i<=4;i++)
{
    setTimeout(()=>{
        console.log(i)
    },1000)
}

Output

var used with setTimeout()

Here, i will always be 5 because the setTimeout callbacks share the same reference to i, which ends up being 5 after the loop ends.

Interesting facts about var Drawbacks of using var Conclusion

While var is still valid and widely supported, it’s generally better to use let and const in modern code to avoid pitfalls like hoisting and lack of block scope. However, a thorough understanding of var is indispensable for debugging and maintaining legacy JavaScript projects.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4