#📌 Follow: @rehman_coding 💼 Portfolio: https://rehman-portfolio.netlify.app/ ⚙️ GitHub: https://github.com/MuhRehman 💎 LinkedIn: https://w
Explore tagged Tumblr posts
Photo
SCOPES IN JAVASCRIPT
So, What is Scope?
Scope determines the visibility or accessibility of a variable or other resource in the area of your code.
There are 3 types of scope:
✅ Block scope ✅ Function scope ✅ Global scope
⚡BLOCK SCOPE
Before ES6 (2015), JS had only Global Scope and Function Scope, ES6 introduced two important keywords: let and const which provide Block Scope in JS. Var keyword can NOT have block scope.
A Block Scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it’s a block. It means that these variables exist only within the corresponding block. (all examples are in carousel)
⚡FUNCTION SCOPE
JavaScript has function scope: Each function creates a new scope. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var, let and const are quite similar when declared inside a function.
⚡LOCAL SCOPE
Variables declared within a JavaScript function, become LOCAL to the function.
⚡GLOBAL SCOPE
There's only one Global scope in the JavaScript document. The area outside all the functions is considered to be in the global scope and the variables defined inside the global scope can be accessed and altered in any other scopes.
❕In JavaScript, objects and functions are also variables.❕
⚡THE LIFETIME OF JAVASCRIPT VARIABLES
The lifetime of a JavaScript variable starts when it is declared. Function (local) variables are deleted when the function is completed.
0 notes