From 73cba4f942de9da875fa4ce8a719166363435377 Mon Sep 17 00:00:00 2001 From: Wemi Moyela Date: Mon, 31 Oct 2022 09:03:39 +0100 Subject: [PATCH] Add content for scope in JavaScript (#2769) --- .../101-javascript-variables/103-scopes/readme.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/content/roadmaps/106-javascript/content/101-javascript-variables/103-scopes/readme.md b/content/roadmaps/106-javascript/content/101-javascript-variables/103-scopes/readme.md index c90894d0f..791a98f27 100644 --- a/content/roadmaps/106-javascript/content/101-javascript-variables/103-scopes/readme.md +++ b/content/roadmaps/106-javascript/content/101-javascript-variables/103-scopes/readme.md @@ -1,6 +1,15 @@ # Scopes -Before ES6 (2015), JavaScript had only Global Scope and Function Scope. ES6 introduced two important new JavaScript keywords: `let` and `const`. These two keywords provide Block Scope in JavaScript. +In JavaScript, scope refers to the visibility of a variable or how it can be used after it is declared. The scope of a variable depends on the keyword that was used to declare it. + +The three types of Scope are Global Scope, Function Scope, and Block Scope. Before ES6 (2015), JavaScript had only Global Scope and Function Scope with the `var` keyword. ES6 introduced `let` and `const` which allow Block Scope in JavaScript. + +Global Scope: Variables declared outside any function or curly braces '{}' have Global Scope, and can be accessed from anywhere within the same Javascript code. `var`, `let` and `const` all provide this Scope. + +Function Scope: Variables declared within a function can only be used within that same function. Outside that function, they are undefined. `var`, `let` and `const` all provide this Scope. + +Block Scope: A block is any part of JavaScript code bounded by '{}'. Variables declared within a block can not be accessed outside that block. This Scope is only provided by the `let` and `const` keywords. If you declare a variable within a block using the `var` keyword, it will NOT have Block Scope. + Free Content JavaScript Scope