xCatalog [1] definition [2] application scenario [3] to Boolean [4] instance method in front of the words
The boolean bool type may be the simplest of three wrapper objects in number, String, and Boolean. Number and string objects have a lot of instance properties and methods, but few are Boolean. In a sense, designing programs for computers is about dealing with Boolean values, and as a 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, the reserved word true and false, respectively representing the two states of true and false
A Boolean wrapper type is a reference type that corresponds to a Boolean value, and using a Boolean object in a Boolean expression can be misleading
var B1 = true ; var b2 = new Boolean (true 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" condition and loop statement
Boolean values are primarily applied to conditional parts of condition and loop statements. For example, if the Boolean value is true in the IF statement, the first paragraph of logic is executed, and if False executes another piece of logic. A comparison that creates a Boolean value is usually combined directly with the statement that uses this comparison
if (A > 1) { // condition is true when executed here }else{ // condition is false, Execute here }
"2" logical operator
Logical operators are also called Boolean operators. A logical non-operator always returns a Boolean value, which is not logical or logical AND operation
You can convert a type to a Boolean by using two logical non-operators at the same time
Console.log (!! 1); // trueconsole.log (!! 0); // falseConsole.log (!! ') ‘); // trueconsole.log (!! "); // false
"3" relational operator
The relational operator is used to test the relationship between two values, and returns TRUE or false depending on whether the relationship exists, and the relational expression always returns a Boolean value, usually using a relational expression in an if, while, or for statement to control the execution process of the program
Console.log (1 > 2); // falseConsole.log (1 < 2); // true
Convert to Boolean
To convert a value to a Boolean value you can use the Boolean () transform function
False value
Values converted to False are called false values (Falsy value), and these 7 values include undefined, NULL, +0,-0, NaN, False, "" (An 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, whereas 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 (")); // 0 Console.log (Boolean (")); // falseConsole.log (Boolean (")); // true
In addition to these 7 false values, the other values are converted to Boolean values that are true, also known as Truth (truthy value)
[Note] All objects (including empty objects) are converted to true, and even false for Boolean object new Boolean (False) is also true
Console.log (Boolean ({})); // trueconsole.log (Boolean ([])); // true Console.log (Boolean(new Boolean (false))); // trueconsole.log (Boolean (false)); // falseConsole.log (Boolean (new Boolean (null))); // trueconsole.log (Boolean (null)); // false
Instance method
The Boolean object is the wrapper type that corresponds to the Boolean value, and the three methods that inherit the generic method of the object objects 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 original Boolean value of Boolean (True or FALSE)
Console.log (true . ValueOf ()); // true console.log (true . toString ()); Span style= "color: #008000;" >// console.log (true . toLocaleString ()); // console.log (Boolean ( // false Console.log (Boolean (false ). ToString ()); // Console.log (Boolean (//
Resources
"1" Es5/boolean object Https://www.w3.org/html/ig/zh/wiki/ES5/builtins
"2" Ruan one peak JavaScript standard reference Tutorial--Grammar Overview http://javascript.ruanyifeng.com/
"3" W3school-javascript Advanced Tutorial--boolean Object http://www.w3school.com.cn/
"4" JavaScript Definitive Guide (6th edition) 3rd Chapter type, value, and variable
"5" JavaScript Advanced Programming (3rd Edition), chapter 3rd Basic Concepts
"6" JavaScript DOM Programming Art (2nd Edition), chapter 2nd JavaScript syntax
"7" javascript Revelation Chapter 12th Boolean ()
JavaScript Type System-Boolean Boolean type