The previous words
Boolean type is probably the easiest of the three wrapping objects Number, String, and Boolean. Number and String objects have a large number of instance properties and methods, but Booleans have very few. In a sense, designing programs for computers is to deal with Boolean values. As the most basic fact, all electronic circuits can only recognize and use Boolean data. This article will introduce the Boolean type
definition
Boolean type represents a logical entity, which has only two values, and reserves the words true and false, respectively representing the two states of true and false.
Boolean wrapper type is a reference type corresponding to a 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 types are mainly used in the following scenarios:
【1】Conditional and loop statement
Boolean values are mainly used in the conditional parts of conditional and loop statements. For example, in an if statement, if the boolean value is true, execute the first segment of logic, and if false, execute another segment of logic. Usually a comparison that creates a boolean value is directly combined with a statement that uses this comparison
if(a > 1){//If the condition is true, execute here }else{//If the condition is false, execute here }【2】Logical operator
Logical operators are also called Boolean operators. Logical non-operators always return boolean values, while logical or and logic and operations are not the case
Use a logical non-operator at the same time, which can convert the type to a boolean
console.log(!!1);//trueconsole.log(!!0);//falseconsole.log(!!' ');//trueconsole.log(!!'');//false
【3】Relational operator
Relational operators are used to test the relationship between two values, and return true or false according to whether the relationship exists. Relational expressions always return a boolean value. Relational expressions are usually used in if, while or for statements to control the execution process of the program.
console.log( 1 > 2);//falseconsole.log( 1 < 2);//true
Convert to Boole
Convert a value to a Boolean value to use the Boolean() transformation function
Fake value
The value converted to false is called a false value. These 7 values include undefined, null, +0, -0, NaN, false, "" (empty string)
console.log(Boolean(undefined));//falseconsole.log(Boolean(null));//falseconsole.log(Boolean(0));//falseconsole.log(Boolean(-0));//falseconsole.log(Boolean(NaN));//falseconsole.log(Boolean(''));//falseconsole.log(Boolean(false));//false[Note] In the Number() method, both the hollow string and the blank string are converted to 0, while in the Boolean method, the empty string "" is converted to false, and the blank string "" is converted to true
console.log(Number(''));//0console.log(Number(' '));//0console.log(Boolean(''));//falseconsole.log(Boolean(' '));//trueIn addition to these 7 false values, the other values converted to boolean values are true, also called true value (truthy value).
[Note] The conversion result of all objects (including empty objects) is true, and even the boolean object corresponding to false is true.
console.log(Boolean({}));//trueconsole.log(Boolean([]));//trueconsole.log(Boolean(new Boolean(false)));//trueconsole.log(Boolean(false));//falseconsole.log(Boolean(new Boolean(null)));//trueconsole.log(Boolean(null));//falseExample method
The Boolean object is a wrapper type corresponding to a Boolean value, inheriting the three methods of the Object object's general methods toString(), toLocaleString(), and valueOf().
【toString()】
toString() method returns the string value of Boolean ('true' or 'false')
【toLocaleString()】
toLocaleString() method returns the string value of Boolean ('true' or 'false')
【valueOf()】
The valueOf() method returns the original Boolean value (true or false) of Boolean
console.log(true.valueOf());//trueconsole.log(true.toString());//'true'console.log(true.toLocaleString());//'true'console.log(Boolean(false).valueOf());//falseconsole.log(Boolean(false).toString());//'false'console.log(Boolean(false).toLocaleString());//'false'
The above is the full description of the detailed explanation of the Boolean type of the JavaScript type system introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time!