Detailed Explanation of Boolean Boolean Type of JavaScript Type System

  • 2021-07-01 06:13:29
  • OfStack

Previous words

The Boolean Boolean type is probably the simplest of the three wrapper objects Number, String, and Boolean. Number and String objects have a large number of instance properties and methods, while Boolean has few. In a sense, designing programs for computers is dealing with Boolean values. As the most basic fact, all electronic circuits can only recognize and use Boolean data. This article introduces Boolean Boolean types

Definition

The Boolean Boolean type represents a logical entity with only two values, preserving the words true and false, which represent true and false states, respectively

The Boolean wrapper type is a reference type corresponding to a Boolean value, and the use of an Boolean object in a Boolean expression is misleading


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 scenario

Boolean types are mainly used in the following scenarios:

'1' Conditions and Loop Statements

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


if(a > 1){
// The condition is true Execute this 
}else{
// The condition is false Execute this 
}

"2" logical operator

Logical operators are also called Boolean operators. Logical non-operators always return Boolean values, while logical OR and logical AND operations do not

You can convert a type to Boolean by using a logical non-operator at the same time


console.log(!!1);//true
console.log(!!0);//false
console.log(!!' ');//true
console.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 1 Boolean value. Usually, relational expressions are used in if, while or for statements to control the execution flow of programs


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

Convert to Boolean

You can use the Boolean () transition function to convert a value to a Boolean value

False value

The values converted to false are called false values (falsy value), and these seven values include undefined, null, +0,-0, NaN, false, "" (empty string)


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(''));//false
console.log(Boolean(false));//false

[Note] In the Number () method, both the empty 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(''));//0
console.log(Number(' '));//0
console.log(Boolean(''));//false
console.log(Boolean(' '));//true

In addition to these seven false values, all the other values converted to Boolean values are true, also known as true values (truthy value)

[Note] All objects (including null objects) are converted to true, and even the Boolean object new Boolean (false) corresponding to false is 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 object is a wrapper type corresponding to Boolean value, which inherits three general methods of toString (), toLocaleString () and valueOf () of Object object

"toString ()"

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

"toLocaleString ()"

The toLocaleString () 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());//'true'
console.log(true.toLocaleString());//'true'
console.log(Boolean(false).valueOf());//false
console.log(Boolean(false).toString());//'false'
console.log(Boolean(false).toLocaleString());//'false'

The above is the site to introduce the Boolean Boolean type of JavaScript type system detailed description of all, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: