Saturday, 8 December 2018

How to declare a global variable in a document?

When you declare any variable outside of a function, then it is global to the current document, and all the code in the document can access this global variable.

HelloWorld.js

var x = 10;

function increment_x(){
  x++;
}

function multiply_x_by_10(){
  x *= 10;
}

function print_x(){
  console.log("Value of x is : " + x);
}

increment_x();
print_x();

multiply_x_by_10();
print_x();

Output
Value of x is : 11
Value of x is : 110




Previous                                                 Next                                                 Home

No comments:

Post a Comment