Home > Web Front-end > JS Tutorial > body text

How to convert javascript string to boolean value

醉折花枝作酒筹
Release: 2023-01-07 11:44:42
Original
12620 people have browsed it

You can use Boolean() in js to convert a string into a Boolean value, and the syntax format is "Boolean (value to be converted into a Boolean value);". If the argument is omitted or set to 0, -0, null, "", false, undefined, or NaN, the object returns false; otherwise, true.

How to convert javascript string to boolean value

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

Boolean(); If the parameters are 0, null and no parameters, it returns false; if there are parameters, it returns true.

Boolean("");  //输出为:false
Boolean(null);  //输出为:false
Boolean(0);  //输出为:false 
Boolean("hi");  //输出为:true
Boolean(100);  //输出为:true
Boolean(new  Object());  //输出为:true
Copy after login

Boolean objects represent two values: "true" or "false".

Syntax

Boolean(value);		//转换函数
Copy after login

Parameters

Parameter value The value stored by the Boolean object or the value to be converted into a Boolean value.

Return value

When called as a constructor (with operator new), Boolean() will convert its argument to a Boolean value and return a Boolean containing that value Boolean object.

If called as a function (without operator new), Boolean() will only convert its argument to a primitive Boolean value and return this value.

Note: If the value parameter is omitted, or is set to 0, -0, null, "", false, undefined, or NaN, the object is set to false. Otherwise set to true (even if the value argument is the string "false").

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert javascript string to 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template