Toggle navigation
Genres
Frontend (2)
JavaScript (6)
Database (2)
Linux Server (3)
Web Apps (4)
Misc (4)
Search
List
New Tutorials
Last Modified Tutorials
</>
Code examples
code
smiko
javascript
advanced
variables
13var_local_global_inside_function2.js
13var_local_global_inside_function2.js
/** * How to define local and global variable inside function. * Notice that if we don't call function with fja() then y returns error in global scope. */ function fja() { var x=5; //local variable y=8; //global variable when function is called with fja() console.log('in fja x='+x); console.log('in fja y='+y); } function fja2() { fja(); //transfer y inside fja2 //console.log('in fja2 x='+x); //ReferenceError console.log('in fja2 y='+y); } fja2(); //propagate y to global scope //console.log('global x='+x); //ReferenceError: x is not defined console.log('in global y='+y);
Reload page
Preview
W3C validation
Edit Code
JS Console