Some Tricky questions in JavaScript that are Easy to answer 😵

Rahat Chowdhury
4 min readMay 7, 2021

Truthy & Falsy Values :

Truth & False are the most common rivals in the whole world, ain’t they? And we see more false than truth in real world. In JavaScript , this case is the same. The number of falsy values is larger than truthy values.

In JavaScript, all numbers(either positive or negative) except 0 are truthy, you can check it out like this

let num = 0;

if(num) { console.log(‘la la la’); } else { console.log(‘he he he’); } here the condition asks if the value is true or false, which means the output will be he he he if false, otherwise will give output la la la , you can change the value of num again & again to check falsy or truthy.

Again, undefined , null , NaN , empty strings ‘’ these are all falsy values whereas white-space string “ ” , '0' or any other string is truthy. It means if there is a value in string that string is truthy. Try this out to get the proof

let anyString = '';

if(anyString) { console.log(‘la la la’); }

else { console.log(‘he he he’); }

in this case this will give output he he he but you can change the value & check it out yourself.

Null vs Undefined :

Undefined :

In JavaScript , undefined is a keyword which defines its own value as undefined. Sometimes. we declare a variable or constant without initialization & later somehow we forget about that . So when we want their value in the console they give us undefined as the result. So, undefined means not defined yet, does that make sense? Okay cool!

Null :

Null is the cousin of Undefined. I mean they are pretty much same as values but they are not same. null is as null (lonely) as it sounds , cause no one really cares about null (like me)😔

Okay let me simplify. Suppose you declare a variable let a; Now if you type console.log(a) this gives us undefined Why? Cause, you just declared a & forgot about him, So, he became angry 😣

However, If you type let a = null; & then console.log(a) now it will give us null

So, you can think like this ,undefined is unintentional mistake , on the other hand null is intentional. If you declare a variable without initialization, the value of it remains undefined until you initialize it. But if you intentionally initialize its value as null then its value remains null.

Double Equal(==) vs Triple Equal(===) :

Both are equal checking operators but a little bit different. Let’s see some examples with them. if(a == b) this checks whether the two values are same or not (both a & b might not be the same type)

whereas if(a === b) checks exactly the same condition but here type of a & b also is a factor.

Suppose a = '4' which is a string & b = 4 which is a number , if we compare these two with == operator, if(a == b) this condition is true but on the other hand, the === operator, if(a === b)checks the value and return false cause a string type and a number type is not a match! (like me & my girlfriend 😔)

(jokes apart) To be double equal ==they don’t see their type, if the values are same, they match, but to be triple equal ===they have to be of same type

Thus, '33' == 33gives true & '33' === 33 false, simple as that.

Scope :

I am gonna jump into examples directly for this.

function checkingVariable() {

let a = 45;

console.log(a);

} this returns 45

but if you write this

function checkingVariable() { let a = 45; } console.log(a); this will give an error. Why? That’s where scope matters. In these two examples we declared a inside the function , so the scope of a is inside the function only which means a can be accessed from anywhere inside the function but a is not recognized anywhere outside the function. That’s why the first example gives output 45 & the second example gives error. This goes same for the const a = 45; in line 2 . Let’s look into another example with var :

let a = 100;

function checkingVariable() { a = 45; }

console.log(a); this gives us output 45 because a is declared outside the function which means a is recognized anywhere outside & inside the function. So, scope is an area in which a variable can be recognized or accessed.

Closure :

When a function returns another function which returns a value accessed from it’s outer-scope, there is created a closed environment , this is called closure. Let’s see an example :

function outerFunction() {

let value = 0;

return function() { value++; return value; }

}

const callFunc = outerFunction();

console.log(callFunc());

--

--

Rahat Chowdhury

Junior Front-End Developer trying to learn & broaden the knowledge