Variables In JavaScript By Sagar Jaybhay

2 Types of Variables In JavaScript

  • Local Variable
  • Global Variable

In JavaScript, there are 2 types of variables. Local variables and global variables.

Local Variable In JavaScript

This variables mean the variables which are declared within a function. These variables have a local scope it means it will accessible only inside the function.

The variable is created when function starts and deleted when function executed completely.

Variables In JavaScript By Sagar Jaybhay
Local Variables In JavaScript

function LocalVariables() {
    var Greetings = "Hello,";
    var myName = "Sagar Jaybhay";
    console.log(Greetings + " " + myName);
}

LocalVariables();

console.log("OutSide the function:- " + Greetings + " " + myName);

If you see above code when we call function it will print the variable values on console, but for second-line it throws an exception which is

LocalandGlobal.js:9 Uncaught ReferenceError: Greetings is not defined   at LocalandGlobal.js:9

Global Variables in JavaScript

Global variables are those variables that are declared outside a function. These Variables have global scope it means all functions, scripts can access these variables.

The lifetime of global variable is until page is open in browser when it is close global variables are removed from memory.

Variables In JavaScript By Sagar Jaybhay
Global Variables In JavaScript

var Greetings = "Hello,";
var myName = "Sagar Jaybhay";

function LocalVariables() {
    console.log(Greetings + " " + myName);
}

LocalVariables();

console.log("OutSide the function:- " + Greetings + " " + myName);

In this above example variables are declared outside of this function and it is accessible inside the function and outside of function also.

Points To Remember about Variables in JavaScript

If you assign value to a variable which is not been declared it also becomes a global variable.

function GlobalVariables() {
     Greetings = "Hello,";
     myName = "Sagar Jaybhay";
    console.log(Greetings + " " + myName);
}

GlobalVariables();

console.log("OutSide the function:- " + Greetings + " " + myName);

Variables In JavaScript By Sagar Jaybhay
Global and Local Variable in JavaScript

In the above code, we have variables inside that function but we not used var keyword before this variable names and we assign the value for them.

Now, these variables worked as global variables and we can access this outside of this function even though they present inside of that function.


Local variables can have the same name as the global variable. If you change the value of one variable is not affect on the other variable. If you changed local variable value and access inside that function you will get local copy of that variable but when you access this variable outside of function you will get global value.

Variables In JavaScript By Sagar Jaybhay
Local Variables and Global Variables In JavaScript

var myName = "Mr.Sagar Jaybhay";

function GlobalLocalVariables() {
     Greetings = "Hello,";
     myName = "Sagar Jaybhay,123";
    console.log(Greetings + " " + myName);
}

GlobalLocalVariables();

console.log("OutSide the function:- " + Greetings + " " + myName);

Braces: In JavaScript braces not creates the scope for variable like C#, java.

Variables In JavaScript By Sagar Jaybhay
Braces In JavaScript

var number = 1000;


if (number > 101) {
    var SecondNumber = number;
}


console.log(SecondNumber);

In this code we defined SecondNumber inside braces of if condition then also it is accessible outside of that braces it means Javascript braces doesn’t create scope for variables. Whatever the variables are declared inside the braces are treated as a global variable.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *