Hackforge Academy

Category: React • Beginner

Published on 18 Feb 2026

Explanation

#white-var is a keyword used to declare variables #white-in JavaScript. It was used before ES6 and #white-has function scope.

Code Example

var name = "Praveen";
console.log(name);

Explanation

#white-Variables declared with var are scoped to the #white-entire function, not the block.

Code Example

function test() {
  if (true) {
    var x = 10;
  }
  console.log(x); // Accessible here
}

test();

Explanation

#white-var does not support block scope. Even if #white-declared inside a block, it is accessible outside #white-the block within the function.

Code Example

if (true) {
  var a = 5;
}

console.log(a); // Works

Explanation

#white-Variables declared with var can be reassigned with #white-new values.

Code Example

var count = 1;
count = 2;
console.log(count);

Explanation

#white-You can redeclare the same variable using var #white-in the same scope without error.

Code Example

var age = 20;
var age = 30;
console.log(age);

Explanation

#white-var variables are hoisted to the top of #white-their scope and initialized with undefined.

Code Example

console.log(x); // undefined
var x = 10;
console.log(x);

Want structured learning with real projects?

Join our Weekend Live Workshop and become job-ready faster.