The JavaScript Programming Language

Global Object

Global Object

When browser load new web page a global object is created with its

  • global properties: undefined, Infinity, NaN
  • global functions: isNaN( ), parseInt( ), eval( )
  • constructor functions: Date(), RegExp(), String(), Object(), Array()
  • global objects: Math, JSON

Also all variables defined globally are properties of this global object.

Client-side: var Window

Server-side (NodeJS): var Global

 

 

// NodeJS - global variable inside function     055global_object_node.js

function fja() {
  x = 5; //global variable
  global.z = 21; //global variable
}
fja();

console.log(x); //5
console.log(z); //21

 

 

 

// Browser -global variable inside function    055global_object_browser.js

function fja() {
  x = 5; //global variable
  window.z = 21; //global variable
}
fja();

console.log(x); //5
console.log(z); //21