DEV Community

Cover image for Scope, Block Scope, Global Scope, Global Variable,Double equal (==) vs Triple equal (===)
Kaniz Mokammel Mim
Kaniz Mokammel Mim

Posted on

Scope, Block Scope, Global Scope, Global Variable,Double equal (==) vs Triple equal (===)

Scope, Block Scope, Global Scope, Global Variable,Double equal (==) vs Triple equal (===)

Scope : Scope is very important in programming. Scope refers to everything contained within "{}". Variable functions within a scope can only be accessed within that specific scope .

Image description
Everything contained in the picture in this image is bound within this particular scope. No access will be available to them outside the scope.

Image description
Whenever I try to log the console outside the scope of the "result" within the, the result is not defined as being outside the scope. So variables declared within a JavaScript function, become Local Scope to the function.

Global Scope and Global Variable : If a variable is declared inside a JavaScript file outside of a local scope, it may be accessible from all locations in that file, this is called global scope. In this picture, we can see the “first” variable is declared on top of the function and it is not bound by any "{}". So we can use the "first" variable anywhere in the file So. it is within the global scope and the variable is a global variable.

Image description

Block Scope: Let and const are the two keywords that provide block scope in JavaScript.The new JavaScript ES6 introduces these two important keywords. So the variables using let and const can only be accessed within their scope.

Image description

In this picture I am using the const keyword to declare a variable . So this keyword maintains block scope. Since I logged the 'blockVariable' console between {} for which the variable ran on the console.
But when I log the 'blockVariable' console outside of {}, the variable cannot find the console.

Image description

Double Equal(==) and Triple Equal(===) : Double Equals ( == ) checks for value equality only. And the other hand, Triple Equals ( === ) will verify whether the variables being compared have both the same value AND the same type .

Image description
In this picture , I am declaring two variables, one is number and another is string. Than I am using double equal (==) for compare these two variables. Double equals can not compare them strictly. It just matches them and gives the answer.

On the other hand, when I can using triple equal(===), here the answer is “both numbers are not equal” ,because triple equal(===) can check them strictly.This means both the type and the **value **we are comparing have to be the same.

Image description
If we have to compare an argument, we use triple equals, because it also compares type and value.So the result gives absolutely right. That's why programmers use Triple Equal.

Top comments (0)