Home  >  Article  >  Web Front-end  >  How to convert javascript string to boolean value

How to convert javascript string to boolean value

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-21 14:54:4912606browse

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

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

Syntax

Boolean(value);		//转换函数

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!

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