JavaScript The Tricky Parts🙄: Part-2

Hey Programmers What are you doing now ?

Did you read first sequel JavaScript The Tricky Parts: Part-1 .If no , please read it first & continue from here.

Hey , I am Samayun Chowdhury speaking here to make your coding life easier, smarter & comfortable. I will talk about most critical parts of javascript in this series . So don’t waste time . Take a glass of coffee & read my blog .

Here I am covering 10 tricky topics of javascript .

⚔️ Truthy & Falsy Value

‘’ or “” (Empty String) , false , 0 , NaN, undefined, null are falsy values .

& rest are truthy values . These are false too 😳

console.log({} == {}) console.log([] == [])

⚔️ Equality Check Operator

strict equality === operator checks operands value & type . Doesn’t apply coercion.

// loose equality vs strict equality
"55" == 55 // -> true
"55" === 55 // -> false
console.log("2" == 2) // true
console.log ("2.04" == 2.04) // true
console.log(true == 1) //true
console.log("0" == false) //true
console.log(2 === 2) // true
console.log("Hello" === "Hello") // true
console.log ("2.04" === 2.04) // false
console.log(true === 1) //false

⚔️ Global & Local Scope

JavaScript Function Scope

In JavaScript there are two types of scope:

  • Local scope
  • Global scope

JavaScript has function scope: Each function creates a new scope.

Scope determines the accessibility (visibility) of these variables.

Variables defined inside a function are not accessible (visible) from outside the function.

Local JavaScript Variables

Local variables have Function scope: They can only be accessed from within the function.

// code here can NOT use carName

function getCarname() {
var carName = "Lamborgini";

// code here (inside function) CAN use carName
// code here can NOT use carName

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts, and deleted when the function is completed.

Global JavaScript Variables

A global variable has global scope: All scripts and functions on a web page can access it.

Automatically Global Variable

This code example will declare a global variable carName, even if the value is assigned inside a function.


// code here can use carName

function myFunction() {
carName = "Volvo";

Strict Mode

⚔️ Closure, encapsulation, private variable

Global variables can be made local (private) with closures 👁‍ .

Variable Lifetime

Global variables live until the page is discarded, like when you navigate to another page or close the window.

Local variables have short lives. They are created when the function is invoked, and deleted when the function is finished.

const add = (function(){
var counter = 0;
return function(){
counter += 1;
return counter;

A function returns another function means Closure .

Hiding some variables or making them private or make global variables hidden(private) is called encapsulation .