From the course: Learning the JavaScript Language

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Functions and scope

Functions and scope

- [Instructor] In this video, we're going to talk more about variable scope and how functions relate to it. Here's an example. I'll create a variable called myNum and set it to 32 and another one called myResult which will set to success. Then I can write a function, or in this case, I'm going to copy and paste a function. It's called randomizer and it takes a limit as its argument. Inside the function, I create a variable called randomNumber and assign A randomly generated number to it. Then I create another variable, which is called myNum, just like the one we created out here originally, outside the function, and then assign that the value of the random number which is between zero and that limit that was passed in. Then we're going to log a couple of lines before returning myNum. So back here, I set myNum to 32 and myResult to success and now we're going to invoke this randomizer function, passing in 10 as the limit.…

Contents