- true and false
Much like c and c++, but unlike Java, what is considered true or false has a wide range in JS. All objects (except empty strings) and non-zero numbers are considered true. Empty strings, zero, null, and undefined are treated as false.
undefined is a special value. All variables are undefined when no value is assigned. clear? :) Also, all functions that don't return a value actually return undefined. In fact, it is a keyword. Therefore, the following code is valid:
var a = undefined;
In fact, it is similar to the following
var a;
- Value forced
In js, when you try to do impossible things with values, js will try to make them compatible and output some meaningful results.
For example: ! 0 is actually true for booleans, ! is generally only used with booleans. ' 2 ' * 1 is actually the number 2, because * cannot be used on strings. However, ' 2 ' + 1 is the string 21, because since there is a string, the number is coerced to a string.
There is a small suggestion. You can use this - var hasChildren = !!children.length; This will set hasChildren to the appropriate boolean value.