Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
What's going to happen is we will jump into the function, we will create the variable, and we will pop up the alert box with the contents of that variable which is 500. Control returns to right after we called it and if try and pop up the content of x again, this will be regarded as undefined. It will say I don't know what x means, because the x variable is not available outside simple function. This is because variables declared inside the functions are referred to as local variables.
They have local scope. They are only available within the opening and closing curly braces of that function. If I need to have the contents of this visible outside, I could do a couple of things. One is I could return a value using the Return keyword and we saw that little earlier. The other way that I could do it do is to declare, to create the variable outside the function. So up at the top I create var x. That's outside all functions and that's referred to as a global variable. That means we could just use the word x to refer to it anywhere, whether I'm inside a function or outside a function.
But with other languages variable scope can apply on those levels too. So you have to be quite careful about where your variables are defined and who can get to them.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99323 Viewers
56 Video lessons · 112585 Viewers
71 Video lessons · 81405 Viewers
131 Video lessons · 39078 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.