What is the output of following code?
if({}) {
console.log("I am true");
} else {
console.log("I am false");
}
----o----
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 totrue
in boolean.