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 Local Variable :
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.
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"
console.log(Visionfortech_global);
o/p : undefined
console.log(Visionfortech_local);
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; console.log(js_local_variable); // output: 2 } console.log(js_local_variable); // output: 1
var global_variable = 1;var javascript_global_variable = 2;if (a === 1) {var global_variable = 11; // the scope is globallet javascript_global_variable = 22; // the scope is inside the if-blockconsole.log(global_variable); // 11console.log(javascript_global_variable); // 22}console.log(global_variable); // 11console.log(javascript_global_variable); // 2
Enjoy....!!!!
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
Comments
Post a Comment