From the course: Master Next.js: Elaborate Hands-On Web Development, React Basics, Advanced Next.js, and Deployment

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

How scope works in JavaScript

How scope works in JavaScript

OK, so we looked at function hoisting and we mentioned this concept of scope. We said that JavaScript will move the functions, only declarative functions, or hoist them to the top in its current scope. We briefly defined the scope for this file. We said, well, if we only have this one file and there's only one function and one console.log, then the scope here really is the global scope. So even if it's below, it's not going to break. It's still going to return not undefined because it's being hoisted. But what the heck is FunctionScope really? And how does it work in JavaScript exactly? It's not complicated, but it's extremely important. So let's review FunctionScopes together. So we're going to make a new file. And we can call this function-scope.js, just one s, Sorry, and we can close the hoisting one. You can save the changes. And let's talk about function scopes. So function scopes. So variables in a function cannot be accessed from anywhere outside the function. You know, let's…

Contents