Following code will output 'true', that means Array() is true. In Python, list() is False, is this just because the language designer's preference?
document.write("<p>Array() is " + (Array() ? "true" : "false") + "</p>");
Following code will output 'true', that means Array() is true. In Python, list() is False, is this just because the language designer's preference?
document.write("<p>Array() is " + (Array() ? "true" : "false") + "</p>");
This is because javascript coerces the value of Array() into a boolean. This is what's referred to as "truthiness", and in javascript, truthy values include any valid object. Array() produces a valid object, and therefore evaluates as a true value in a boolean expression.
Null, undefined, NaN, 0, and the empty string "" evaluate as false.
Why? Because the ECMAScript spec says so.
Array() is truthy. In other words, it has a value, and the value is not false, null or undefined. There are many lists of what is truthy and not out there on the web for javascript. Just as an example, a more 'standard'/'accepted' way of creating a new array is by using an array literal - []. If you put this in your console, you'll get "true" as well.
console.log(!!([]));
Array() returns array here (though empty), try (Array().length ? "true" : "false")
Array().length is an integer, not a boolean..length?false in one language and true in another? Your answer suggests that instead of one implicit Boolean conversion Array() ? "true" : "false" ⇒ Boolean({}) ? "true" : "false", OP should use a different implicit Boolean conversion Array().length ? "true" : "false" ⇒ Boolean(0) ? "true" : "false". It happens to have a different result because it's a different type conversion, but it's still an implicit conversion.