Some Tricky Things In JavaScript
By: Sadman Sakib Mugdho
Tags: Programming Languages, Programming, Javascript Development, Javascript Tips, JavaScript

Some Tricky Things In JavaScript

Photo by AltumCode on Unsplash

Truthy and Falsy

Every number in JavaScript is truthy except “0”. If a variable contains “0”, then it’s a falsy value. You can check them…

var num = 0; if (num){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false num = 1; if (num){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is true num = -1; if (num){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is true

So here we can see that all natural numbers are truthy except zero(0).

In JavaScript, every string is truthy except an empty string (“”). If a variable contains an empty string, then it’s a falsy value. Here is an example…

var name = "Mugdho"; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is true name = ""; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false

Remember, if a string contains a space (“ ”) or zero (“0”), then it’s a truthy value. Here is an example…

var name = " "; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is true name = "0"; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is true

Some other falsy values are null, undefined, NaN, false, etc. See the example below.

var name = null; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false

name = undefined; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false

name = NaN; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false

name = false; if (name){

console.log("Condition is true")

} else {

console.log("Condition is false")

} // Output: Condition is false

Null vs Undefined

If you don’t assign a value to a variable, then its value is “undefined”. “undefined” is used when the value is unintentionally not assigned.

let num; console.log(num); // Output: undefined

On the other hand, when you want to declare a variable which value is empty, you use “null”. “null” is used when the value is intentionally not assigned.

let foods = null; console.log(foods); // Output: null

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

You might have seen both double equal and triple equal in JavaScript. But do you what’s the difference between them? Follow the code below…

let num1 = 2;

let num2 = "2"; if (num1 == num2){

console.log("Condition is true")

}else {

console.log("Condition is false")

} // Output: Condition is true if (num1 === num2){

console.log("Condition is true")

}else {

console.log("Condition is false")

} // Output: Condition is false

Now you might be asking why the results are different?

The answer is, double equal doesn’t check the type of a value. If there is string 2 (“2”) and number 2 (2), it doesn’t complain. On the other hand, triple equal checks both type and value. That's why we got false last time. If we change both values to the same type, the condition will be true. Follow the example…