What is the difference between global and local variables?

The main difference between global and local variables is where they can be used in the program. Using a variable globally means that it WILL suffer changes everytime  its value changes, whether this happens inside or outside a function. It is declared inside the script tags but outside any function. Local variables can only be used inside the function they were created in and WILL NOT be recognised outside that specific function. Be careful with nested expression too, because a variable declared inside a nested expression can only be used from the place in was declared in inwards, but not outside the first expression. 

DP
Answered by David P. Javascript tutor

1081 Views

See similar Javascript Mentoring tutors

Related Javascript Mentoring answers

All answers ▸

How are objects created in arrays


I've just heard about objects. How do I use them and do they work with Arrays?


If I do var a=3 and var b=4 and then do var c=a+b it tells me the answer is 34. Why?


How do you create objects in javascript?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences