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
12var_local_global_inside_function.js
12var_local_global_inside_function.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('local x='+x); console.log('local y='+y); } fja(); //try to comment this line and you will see how console.log('global y='+y); produce error //console.log('global x='+x); //ReferenceError: x is not defined console.log('global y='+y); //ReferenceError: y is not defined when function is not called with fja();
Reload page
Preview
W3C validation
Edit Code
JS Console