Some Tricky questions in JavaScript that are Easy to answer 😵

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 are truthy, you can check it out like this

here the condition asks if the value is true or false, which means the output will be if false, otherwise will give output , you can change the value of again & again to check falsy or truthy.

Again, , , , empty strings these are all falsy values whereas white-space string , 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

in this case this will give output 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 as the result. So, 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. is as (lonely) as it sounds , cause no one really cares about null (like me)😔

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

However, If you type & then now it will give us

So, you can think like this , is unintentional mistake , on the other hand is intentional. If you declare a variable without initialization, the value of it remains until you initialize it. But if you intentionally initialize its value as 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. this checks whether the two values are same or not (both a & b might not be the same type)

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

Suppose which is a string & which is a number , if we compare these two with operator, this condition is true but on the other hand, the operator, 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, gives true & false, simple as that.

Scope :

I am gonna jump into examples directly for this.

this returns

but if you write this

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

this gives us output because is declared outside the function which means 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 :

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