From the course: JavaScript: Scope

Unlock the full course today

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

Solution: Define global variable

Solution: Define global variable - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

Solution: Define global variable

- [Instructor] Let's go through the solution of the challenge. Keep in mind that the array you've built could have been anything you wanted. It could have single strings, numbers, objects. As long as they are globally available and we console.log this variable from a function, it all works. So let's take a look at mine. So what I've done here is declare and define an array here. So the array of warriors with properties named type, weapon, agility. And I had three of 'em. And then as you see in the notes, this is globally available. So I could use this array anywhere in this file or if I've exported this function and then imported it into another file, I could use it as well. Now this function is also global because it's at the top of my file. There's no function on top of it so it's not scoped inside of another function. It's available and also it returns into the console the list of warriors here. So if we run…

Contents