Home > Web Front-end > Front-end Q&A > What value is false in JavaScript

What value is false in JavaScript

青灯夜游
Release: 2021-07-22 09:52:09
Original
2744 people have browsed it

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
  • 0 and other forms of numbers such as zeros -0, 0, 0.0 and 0x0 (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.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. 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 uses ToNumber() and may cause confusion due to potential differences. They are effectively divided into three categories:

  • false, 0, -0, "", '' match each other==
  • For example false == "",'' == 0Therefore 4/2 - 2 == 'some string'.slice(11);
  • null, undefined and ==
  • For example, null == undefined but undefined != false
  • It is also worth mentioning that although 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)
  • NaN does not match anything, use == or ===, not even self
    For example NaN != NaN, NaN !== NaN, NaN != false, NaN != null
  • For "strict equality" (===), 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 ToBooleanFunction 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
  • 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.

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

  • {}

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

  • true, 1, "1" and [1] returns true compared 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template