Home  >  Article  >  Web Front-end  >  Parsing Boolean types in JavaScript types

Parsing Boolean types in JavaScript types

怪我咯
怪我咯Original
2017-04-01 09:11:311381browse

Previous words

The Boolean value Boolean type may be three types of packaging objects Number, String and Boolean The simplest one. Number and String objects have a large number of instance properties and methods , but Boolean has very few. In a sense, programming a computer is about working with Boolean values, and as a fundamental fact, all electronic circuits can only recognize and use Boolean data. This article will introduce the Boolean type

Definition

The Boolean type represents a logical entity. It has only two values, the reserved words true and false, which represent true and false respectively. Two states

The Boolean packaging type is the reference type corresponding to the Boolean value. Using Boolean objects in Boolean expressions can easily cause misunderstandings

var b1 = true;
var b2 = new Boolean(true);
console.log(b1,typeof b1);//true 'boolean'
console.log(b2,typeof b2);//Boolean{[[PrimitiveValue]]: true} 'object'
console.log(b1.valueOf(), typeof b1.valueOf());//true 'boolean'
console.log(b2.valueOf(), typeof b2.valueOf());//true 'boolean'

Application scenarios

 Boolean type is mainly used in the following scenarios:

 【1】Conditions and Loop Statement

Boolean values ​​are mainly used in conditional and conditional parts of loop statements. For example, in the if statement, if the Boolean value is true, the first piece of logic is executed, and if it is false, the other piece of logic is executed. Usually a comparison that creates a Boolean value is combined directly with the statement that uses the comparison

if(a > 1){
//条件为true时,执行此处
}else{
//条件为false时,执行此处
}

 【2】logical operator

The logical operator is It's called a Boolean operator. Logical NOT operator always returns a Boolean value, but this is not the case for logical OR and logical AND operations

Using a logical NOT operator at the same time, you can Type conversion to Boolean type

console.log(!!1);//true
console.log(!!0);//false
console.log(!!' ');//true
console.log(!!'');//false

【3】Relational operator

Relational operator is used to test the relationship between two values ​​and returns based on whether the relationship exists True or false, relational expressions always return a Boolean value. Relational expressions are usually used in if, while or for statements to control the execution flow of the program

console.log( 1 > 2);//false
console.log( 1 < 2);//true

Convert to Boolean

To convert a value to a Boolean value, you can use Boolean() transformationFunction

False value

The value converted to false is called a falsy value. These seven values ​​include undefined, null, +0, -0, NaN, false, ""(emptystring)

console.log(Boolean(undefined));//false
console.log(Boolean(null));//false
console.log(Boolean(0));//false
console.log(Boolean(-0));//false
console.log(Boolean(NaN));//false
console.log(Boolean(&#39;&#39;));//false
console.log(Boolean(false));//false

 [Note] In the Number() method, both empty strings and blank strings are converted to 0, while in the Boolean method, the empty string "" is converted to false, and the blank characters Convert string " " to true

console.log(Number(&#39;&#39;));//0
console.log(Number(&#39; &#39;));//0
console.log(Boolean(&#39;&#39;));//false
console.log(Boolean(&#39; &#39;));//true

Except for these 7 false values, all other values ​​converted to Boolean values ​​are true, also called truthy values

[Note] The conversion results of all objects (including empty objects) are true, even the Boolean object new Boolean(false) corresponding to false is also true

console.log(Boolean({}));//true
console.log(Boolean([]));//true
console.log(Boolean(new Boolean(false)));//true
console.log(Boolean(false));//false
console.log(Boolean(new Boolean(null)));//true
console.log(Boolean(null));//false

Instance method

 Boolean The object is a packaging type corresponding to a Boolean value. Inherites the three general methods toString(), toLocaleString(), and valueOf() of the Object object

【toString()】

The toString() method returns a Boolean string value ('true' or 'false')

【toLocaleString()】

toLocaleString() The method returns the string value of Boolean ('true' or 'false')

【valueOf()】

The valueOf() method returns the original Boolean value of Boolean (true or false)

console.log(true.valueOf());//true
console.log(true.toString());//&#39;true&#39;
console.log(true.toLocaleString());//&#39;true&#39;
console.log(Boolean(false).valueOf());//false
console.log(Boolean(false).toString());//&#39;false&#39;
console.log(Boolean(false).toLocaleString());//&#39;false&#39;




The above is the detailed content of Parsing Boolean types in JavaScript types. 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