JavaScript Var

In this post we will learn about JavaScript var, it is a keyword used to declare a JavaScript variable with function scope. Before the introduction of ES6, all keywords in JavaScript were declared using only the “var” keyword. The var keyword is also used to declare variables of global scope.

Syntax:

var variableName = valueOfVar;

Function Scope:Variables declared within a function have function scope and cannot be called outside the function. Variables declared with var are accessible only within this function and its enclosing functions.

Variables declared with the var keyword appear at the top and are initialized with the default value undefine before code execution. Variables declared in global scope outside a function cannot be deleted.

Example 1: In this example, we will declare a global variable and access it anywhere inside the program

JavaScript

var test = 12
function go(){
    console.log(test);
}
go();
Output:
12

Example 2: In this example, we will declare multiple variables in a single statement

JavaScript

var test1 = 12,
    test2= 14,
    test3 = 16
function go(){
    console.log(test1, test2, test3);
}
go();
Output:

12 14 16

Example 3: In this example, we will see the hoisting of variables declared using var

JavaScript

console.log(test);
var test = 12;
Output:
undefined

Explanation:The test variable is already displayed at the top before the program execution starts and the variable is initialized with the default value unknown, so you will get the output without any errors.

Example 4: In this example, we will redeclare a variable in the same global block

JavaScript

var test = 12;
var test = 100;
console.log(test);
Output:
100

Explanation: We did not get any error when redeclaring the variable if we did the same with the let keyword an error would be thrown

Example 5: In this example, we will redeclare the variable in another scope and see how it is the original variable.

JavaScript

var test = 12;
function foo(){
    var test = 100;
    console.log(test);
}
foo();
console.log(test);
Output:
100
12

Explanation: Redeclaring a variable within a different function scope does not generate an error and preserves the variable’s original value.

Example 6: In this example, we will try to delete a global variable declared using va in the ‘use strict’ mode

JavaScript

'use strict';
var test = 12;
delete(test);
console.log(test);
Output:
JavaScript var
Explanation: A variable cannot be configured if it is declared using var at global scope. Therefore, it cannot be deleted using the delete keyword and an error is thrown.

Supported Browser:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Internet Explorer
  • Safari

NOTE: – To clear your concept of var, const, and let please go through How to declare variables in different ways in JavaScript?

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, CodeConfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!