Home> CMS Tutorial> WordPress> body text

How to determine whether it is a Boolean value

WBOY
Release: 2023-08-31 10:25:12
Original
951 people have browsed it

How to determine whether it is a Boolean value

Boolean()The constructor can be used to create Boolean objects as well as Boolean primitives, representingtrueorfalsevalues .

In the code below, I detail the creation of Boolean values in JavaScript.

Example: sample52.html

Copy after login

Boolean()Parameters

Boolean()The constructor converts an argument to a Boolean value (i.e.trueorfalse). Any valid JavaScript value that is not 0, -0,null,false,NaN,undefined, or the empty string ("") will be converted totrue. In the following example, we create two Boolean object values: atrueand afalse.

Example: sample53.html

Copy after login

When used with thenewkeyword, an instance from theBoolean()constructor produces an actual complex object. You should avoid using the Boolean() constructor to create boolean values (instead use literals/raw numbers) because of potential issues related to thetypeofoperator. Thetypeofoperator reports Boolean objects as "object" instead of the original label ("boolean") as you might expect. Additionally, literal/raw values are written faster.


Boolean()Properties and methods

Boolean()The object has the following properties:

Properties (for example,Boolean.prototype;):

  • prototype

Boolean object instance properties and methods

Boolean object instances have the following properties and methods (excluding inherited properties and methods):

Instance properties (e.g.,var myBoolean = false;myBoolean.constructor;):

  • Constructor

Instance methods (e.g.,var myNumber = false;myBoolean.toString();):

  • toSource()
  • toString()
  • valueOf()

Non-original False Boolean objects are converted to True

AfalseBoolean object created from theBoolean()constructor (rather than a primitive value) is an object, and the object is converted totrue. Therefore, when afalseBoolean object is created via theBoolean()constructor, the value itself is converted totrue. In the example below, I demonstrate how afalseBoolean object is always "true".

Example: sample54.html

Copy after login

If you need to convert a non-Boolean value to a Boolean value, just use theBoolean()constructor without thenewkeyword, and the returned value will be the original value instead Not a Boolean object.


Some things are false, everything else is true

Already mentioned, but worth mentioning again as it relates to conversions: if value is 0, -0,null,false,NaN,undefined, or an empty string ("") isfalse. If used in a Boolean context, any value in JavaScript other than the above will be converted totrue(i.e.if (true) {};).

Example: sample55.html

Copy after login

in conclusion

It's critical to know which JavaScript values are simplified tofalseso that you know that all other values are treated astrue.

The above is the detailed content of How to determine whether it is a Boolean value. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!