From the course: Computer Science Principles Lab: JavaScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Variables and scope

Variables and scope - JavaScript Tutorial

From the course: Computer Science Principles Lab: JavaScript

Start my 1-month free trial

Variables and scope

- [Instructor] The scope of a variable or of a function is based on where it's defined. This means that if a variable is defined in a function, it isn't available for you to use outside of the function. I created some code to get us started to look at how scope can affect access to your variables. If you open up the JavaScript file for this movie you will see the following code. This code uses a trick that many programmers use, which is commenting out code. To keep code from running you can add a double-slash before it, making it a comment which the browser would ignore when it tries to run the code. Commenting out code is a good way to test out your code and trying different things without needing to completely remove the code from your program. There are three main things I want to point out in this code. And we'll modify it to illustrate each point. The first is at the top. We create a value called outsidevalue. In this variable is a number with a value of five. Immediately after…

Contents