Member-only story
function and block scopes in JavaScript
function and block scopes — #6 of the 40 Essentials of Javascript
Welcome to another post of this JavaScript essential series. This article aims to explain function and block scopes in JS.
Intro
JavaScript has two fundamental scopes when it comes to variable declarations — function and block. Although they look similar, the differences between and uses of the two are distinct. Understanding these scopes is necessary for writing efficient JavaScript code.
By the way, I have an Instagram Reel on this Article, if you guys prefer that, please click here: Insta Reel
Function scope
Function scope is by far the most common and default scope in JavaScript. This means that any variables declared inside a function will only be accessible within that function and not outside it. A variable — declared within the function is not available outside the function, so it is important to understand the concept of a function.
Variable declared using `var` will ONLY exist within the scope of function it is declared in. However, var created outside a function is globally scoped.