Back to Browse

What is a Scope?

2.8K views
May 27, 2021
13:58

What is a scope? Scope controls how JavaScript variables are organized and accessed by the JavaScript engine. A scope tells where the JavaScript variable and function lives, and where can we access a certain variable or a function and where we can’t. In more simple words, Scope of a variable or a function is the entire region inside which that variable or function can be accessed and used. Types of scope In JavaScript, we have three types of scope – Global scope, function scope and block scope. Global Scope - The top-level code i.e. the variables and functions that are not present inside any other function or block is called as global variables and functions. They have global scope. These global variables and global functions are accessible from anywhere in the program i.e. in all functions and all blocks. Function Scope - Each and every function in JavaScript, creates a scope. The variables and functions declared inside any function are only accessible inside that function. This is also called as local scope. Block Scope - Variables declared inside a block is only accessible inside that block and not outside of it. The point to note here is that, the block scope is only created by variables declared using let or const. Lexical Scoping - Lexical scoping means the way variables are organized and accessed is entirely controlled by the placement of functions and of blocks in the programs code.

Download

1 formats

Video Formats

360pmp415.9 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

What is a Scope? | NatokHD