Javascript Global and Local Variables

Javascript Global and Local Variables

Into this post we are going to discuss difference between how to declare javascript global variable and javascript local variable and its scope

we are going to discuss how can we declare global variable and local varible into Javascript with example.

Javascript global variable vs local variable by visionfortech
Javascript global variable vs local variable by visionfortech

Javascript Global Variable:

If we define var and let variable same globally then let variable will not get added into global window object while var variable will get added to global window object.

So let's declare two variables at the time of window loaded
let Visionfortech_global = "Visionfortech is defined as Global variable"

var Visionfortech_local = "Visionfortech is defined as Local variable"

o/p : undefined

o/p : Visionfortech is defined as Local variable

Javascript Local Variable : 
let js_local_variable = 1;

if (js_local_variable === 1) {
  let js_local_variable = 2;

  // output: 2

// output: 1

Now let see the scope example of Javascript global and Local variable.

var global_variable = 1;
var javascript_global_variable = 2;
if (a === 1) {
var global_variable = 11; // the scope is global
let javascript_global_variable = 22; // the scope is inside the if-block
console.log(global_variable); // 11
console.log(javascript_global_variable); // 22
console.log(global_variable); // 11
console.log(javascript_global_variable); // 2


Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

 If you want to take your Technological Knowledge to the Next Level and For More Technological information Stay tuned to Visionfortech