JavaScript syntax: Difference between revisions

Content deleted Content added
m Reverted edits by 185.112.167.43 (talk): Editing tests (HG) (3.1.20)
Pepkin88 (talk | contribs)
`null` is not an object
Line 289:
===Boolean===
 
[[JavaScript]] provides a [[Boolean data type]] with {{mono|true}} and {{mono|false}} literals. The typeof operator returns the string {{mono|"boolean"}} for these [[primitive types]]. When used in a logical context, {{mono|0}}, {{mono|-0}}, {{mono|null}}, {{mono|NaN}}, {{mono|undefined}}, and the empty string ({{mono|""}}) evaluate as {{mono|false}} due to automatic [[type coercion]]. The [[complement (set theory)|complement]] evaluates as {{mono|true}}, including the strings {{mono|"0"}}, {{mono|"false"}} and any object (except {{mono|null}}). Automatic type coercion by the equality comparison operators (<code>==</code> and <code>!=</code>) can be avoided by using the type checked comparison operators, (<code>===</code> and <code>!==</code>).
 
When type conversion is required, JavaScript converts {{mono|Boolean}}, {{mono|Number}}, {{mono|String}}, or {{mono|Object}} operands as follows:<ref>{{cite web | url=https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators | title=Comparison Operators - MDC Doc Center | publisher=Mozilla | date=5 August 2010 | accessdate=5 March 2011}}</ref>
Line 320:
alert( 1 ? true : false); // true.... only ±0 and NaN are “falsy” numbers
alert("0" ? true : false); // true.... only the empty string is “falsy”
alert(Boolean({})); // true.... all objects are “truthy” except null
</syntaxhighlight>
</div>