JS Daily 4 - Truthy Empty Objects

Last updated on 24 Nov, 2020

What is the output of following code?

if({}) {
  console.log("I am true");
} else {
  console.log("I am false");

Output will be I am true.

Any object in JavaScript, when converted to a boolean value, results in true. It can even be an empty object, as shown in the question.

Since Array is also a type of object, empty array([]) also converts to true in boolean.

--- ○ ---
Joby Joseph
Web Architect