Front.
Boolean booleans may be the simplest of three wrapper objects number, String, and Boolean. Numbers and string objects have a large number of instance properties and methods, but few Boolean. In a sense, for the computer design program is to deal with the Boolean value, as the most basic fact, all electronic circuits can only recognize and use Boolean data. This article describes the Boolean Boolean type
Defined
The Boolean Boolean type represents the logical entity, which has only two values, preserves the word true and false, and represents both the true and false states
A Boolean wrapper type is a reference type that corresponds to a Boolean value, and using a Boolean object in a Boolean expression can easily cause misunderstanding
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
The Boolean type is mainly used in the following scenarios:
"1" conditions and circular statements
Boolean values are mainly applied to conditions and the conditional portion of a circular statement. For example, in an if statement, if the Boolean value is true to perform the first paragraph of logic, if it is false to perform another piece of logic. A comparison of creating a Boolean value is typically combined directly with a statement that uses this comparison
if (a > 1) {
//condition is true, execute here
when}else{
//condition is false
"2" logical operators
The logical operator is also called a Boolean operator. The logical non-operator always returns a Boolean value, which is not the case with logic or logic and operations
Using a logical non-operator, you can convert a type to a Boolean
Console.log (!!) 1);//true
console.log (!!) 0);//false
console.log (!! ' ");//true
console.log (!!"); /false
The "3" relational operator
The relational operator is used to test the relationship between two values, returning TRUE or false based on whether a relationship exists, always returning a Boolean value, usually using a relationship expression in an if, while, or for statement to control the execution process
Console.log (1 > 2);//false
Console.log (1 < 2);//true
Convert to Boolean
Converts a value to a Boolean value using the Boolean () transformation function
False value
The value converted to false is called a false value (Falsy value), which includes 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 hollow string and the blank string are converted to 0, whereas in the Boolean method the empty string "" is converted to false and the white space 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 7 false values, other values are converted to Boolean values that are true, also known as the Truth value (truthy value)
[note] The conversion result for all objects (including empty objects) is 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 (FALSE)) );//true
Console.log (Boolean (False)),//false Console.log (Boolean (null));
//true
Console.log (Boolean (null));//false
Instance method
A Boolean object is a wrapper type that corresponds to a Boolean value and inherits the three methods of the object's generic method tostring (), tolocalestring (), valueof ()
"ToString ()"
The ToString () method returns a Boolean string value (' True ' or ' false ')
"toLocaleString ()"
The toLocaleString () method returns a Boolean string value (' True ' or ' false ')
"ValueOf ()"
The valueof () method returns the Boolean original Boolean value (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 ( False). ToString ());//' false '
Console.log (Boolean (false). toLocaleString ());/' false '
The above is a small series to introduce the JavaScript type system Boolean type of the full description of the classification, I hope to help everyone, if you have any questions welcome to my message, small series will promptly reply to everyone!