In JavaScript, false, 0 and other forms of numbers, etc. Zero ("-0", "0", "0.0" and "0x0"), null, undefined, NaN, "document.all ", "[]", "{}", """" (empty string) and other values are all false values.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
False values in JavaScript
false
0
and other forms of numbers such as zeros -0
, 0
, 0.0
and 0x0
(credit RBT for hexadecimal Form) document.all (only in HTML browsers) This is a weird one. document.all
is a fake object with typeof
as undefined
. It was a Microsoft-proprietary feature in IE prior to IE11, and was added to the HTML specification as a "deliberate violation of the JavaScript specification" so that websites written for IE would not break document.all.something when trying to access it. This is bogus because it if (document.all)
used to be a popular way to detect in IE before conditional comments. See Why document.all
is false? Details
"Falsey" simply means JavaScript's internal ToBoolean
function return false
. ToBoolean
underlies !value, value? ...
: ...; and if (value). Here is its official specification (2018 working draft) (the only change since the first ECMAscript specification in 1997 is the addition of ES6's Symbols, which have always been true):
[]
{}
"" (empty string)
Comparison with == (loose equality)
It is worth talking about the loose comparison of falsy values with ==
, which uses ToNumber()
and may cause confusion due to potential differences. They are effectively divided into three categories:
false, 0, -0, "", ''
match each other==
false == ""
,'' == 0
Therefore 4/2 - 2 == 'some string'.slice(11)
;null, undefined
and ==
ull == undefined
but undefined != false
typeof null
returns 'object', null
is not an object and this is a long-term bug/ Quirks which are not fixed to maintain compatibility. It is not a real object, and the object is real (document.all
"malicious violations" occur when Javascript is implemented in HTML)==
or ===
, not even self NaN != NaN
, NaN !== NaN
, NaN !
= false
, NaN != null
===
), there is no such grouping. Only false === false
. This is one of the reasons why many developers and many style guides (such as standardjs) prefer ===
and almost never use it==
.
True values == false
"Truthy" only means JavaScript's internal ToBoolean
Function return true
. Javascript application quirks to know (and another good reason to prefer ===
over ==
): It is possible that the value is truthy(ToBoolean
returns true), and also == false
.
You may think that this if (value && value == false) alert('Huh?')
is a logical possibility that cannot happen, but for the following situation, it would be possible:
"0"
and '0'
- they are non-empty strings, which is correct, but Javascript will convert = =Numbers match equivalent strings (e.g. 42 == "42"
). Because 0 == false
, if "0" == 0
, "0" == false
. new Number(0)
and new Boolean(false)
- they are real objects, but == sees their values which == false
. 0 .toExponential()
; - a numerical equal object 0[]
, [[]]
and [0]
(thanks to cloudfeet for the JavaScript equality table link) Some Truer Values
These are just values that some might expect to be false, but are actually true.
-1
and all non-zero negative numbers' '
," "
,"false"
, 'null'
... All non-empty strings, including just space strings Anything coming from typeof, always returns a non-empty string, for example:
typeof null
('object'
returns a string due to a long-standing bug/quirk)
typeof undefined
(returns a string 'undefined'
) Any object (document.all
"Malicious violation in the browser ” except; remember that null is not actually an object, despite other suggestions of typeof). Contains:
{}
or
() => {} (any function, including empty functions)
and
Error
(including
new Number(0) and
new Boolean(false)) Any content of
and
[1] returns
true compared to each other
==.
javascript advanced tutorial]
The above is the detailed content of What value is false in JavaScript. For more information, please follow other related articles on the PHP Chinese website!