JavaScript The Tricky Parts🙄: Part-2

Samayun Miah Chowdhury
4 min readMay 8, 2021

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

Truthy & Falsy values are coerced to Booleans when in statements. You can use these statemenst in condition block in switch, if-else,while blocks. You will see this future more & more . So just read now for easier future 😎

‘’ 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

There has two kinds of quality check operator Loose & Strict Equality Opearator. == check operands value & apply automatically coercion ,it doesn’t check operands type .

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

Scope determines the accessibility (visibility) of variables.

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

Variables declared within a JavaScript function, become LOCAL to the function.

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 variable declared outside a function, becomes GLOBAL.

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

Automatically Global Variable

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

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

myFunction();

// code here can use carName

function myFunction() {
carName = "Volvo";
}

Strict Mode

in mid level project if someone creates global variable may conflict with others code , which can be reason for some unexpected errors . Stop automatically creating global variables by using 'use strict' line in the top of code blocks.

⚔️ Closure, encapsulation, private variable

JavaScript variables can belong to the local or global scope.

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;
}
})();
console.log(add());
console.log(add());

A function returns another function means Closure .

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

--

--

Samayun Miah Chowdhury

Fan of JavaScript & Python lover Feels good to introduced as a realtime web application developer sunglasses PWA is my crushwink I do 2 times teeth brush