Predict the output of the following JavaScript code
(function (flag) {
let age = Boolean(NaN === NaN ? false : flag); console.log(age.toString()[Number(flag)]);
})([]);
Quiz Explanation
We have a self-executing function with the parameter/argument is an empty array. Noted that NaN === NaN returns false, then age gets the value flag, which is an empty array. However, the boolean value is true when we call Boolean([]).
The function toString() returns the string true and the Number([]) returns 0. Then we get "t" in the console.log. The correct answer is B.
Keep in mind that Boolean([]) ==> true but Number([]) ==> 0. And sadly NaN === NaN returns false.