Home > Web Front-end > JS Tutorial > Using JavaScript: Detailed solution for converting other types of values ​​into Boolean values_javascript skills

Using JavaScript: Detailed solution for converting other types of values ​​into Boolean values_javascript skills

WBOY
Release: 2016-05-16 17:34:39
Original
1006 people have browsed it

1.Convert using Boolean objects

Copy the code The code is as follows:

var num123 = 123, str = 'abc', o = {name:'test'}, num0 = 0; num123 = Boolean(num123); //true num0 = Boolean(num0); //false str = Boolean(str); //true o = Boolean(o); //true

2. Use two '!' operators, the first '!' converts the value into a Boolean value and takes the non-value of its value, the second '!' converts its Boolean value Reduction is similar to the principle of "a negative makes a positive".

Copy code The code is as follows:

var num123 = 123, str = 'abc', o = {name:'test'}, num0 = 0; num123 = !!(num123); //true num0 = !!(num0); //false str = !!(str); //true o = !! (o); //true

The result obtained is the same as method 1.

Finally, you should know : Any JavaScript value can be converted into a Boolean value. The following values ​​will be converted to false:

Copy code The code is as follows:

undefined,null ,0,-0,NaN,"" //Empty string

It should be noted that is '0', a string that only contains 0 will be converted to true!

Add the special features of null and undefined:

Copy the code The code is as follows:

null == undefined //true null === undefined //false
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