Home>Article>Web Front-end> What value is false in JavaScript

What value is false in JavaScript

青灯夜游
青灯夜游 Original
2021-07-19 16:17:10 2656browse

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.

What value is false in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

False values in JavaScript

  • false
  • 0and other forms of numbers such as zeros-0,0,0.0and0x0(credit RBT for hexadecimal Form)
    - `"", '' and ``` - string of length 0
  • null
  • undefined
  • NaN
  • document.all (only in HTML browsers) This is a weird one.document.allis a fake object withtypeofasundefined. 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 itif (document.all)used to be a popular way to detect in IE before conditional comments. See Whydocument.allis false? Details
    "Falsey" simply means JavaScript's internalToBooleanfunction returnfalse.ToBooleanunderlies!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 usesToNumber()and may cause confusion due to potential differences. They are effectively divided into three categories:

  • false, 0, -0, "", ''match each other==
  • For examplefalse == "",'' == 0Therefore4/2 - 2 == 'some string'.slice(11);
  • null, undefinedand==
  • For example, null == undefinedbutundefined != false
  • It is also worth mentioning that althoughtypeof nullreturns'object', nullis 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)
  • NaN does not match anything, use==or===, not even self
    For exampleNaN != NaN,NaN !== NaN,NaN !=false,NaN != null
  • For "strict equality" (===), there is no such grouping. Onlyfalse === 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 internalToBooleanFunction returntrue. Javascript application quirks to know (and another good reason to prefer===over==): It is possible that the value is truthy(ToBooleanreturns true), and also== false.

You may think that thisif (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"). Because0 == false, if"0" == 0,"0" == false.
  • new Number(0)andnew Boolean(false)- they are real objects, but == sees their valueswhich == false.
  • 0 .toExponential(); - a numerical equal object 0
  • Any similar construct will give you a false value, wrapped in a real type
  • [],[[]]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.

  • -1and 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:

  • {}

  • ##[]
  • function( ){}or() => {}(any function, including empty functions)
  • ErrorandError
  • Any regular expression
  • created with
  • new(includingnew Number(0)andnew Boolean(false)) Any content of
  • any symbol

  • true, 1, "1"and[1]returnstruecompared to each other==.
[Recommended learning:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn