Parse the Boolean Type in JavaScript

Source: Internet
Author: User
The Boolean type can be the simplest of the three packaging objects: Number, String, and Boolean. Number and String objects have a large Number of instance attributes and methods, but few Boolean objects. In a sense, designing a program for a computer is dealing with Boolean values. As a basic fact, all electronic circuits can only recognize and use boolean data. This article introduces Boolean B Previous

The Boolean type can be the simplest of the three packaging objects: Number, String, and Boolean. Number and String objects have a large Number of instance attributes and methods, but few Boolean objects. In a sense, designing a program for a computer is dealing with Boolean values. As a basic fact, all electronic circuits can only recognize and use boolean data. This topic describes the Boolean type.

Definition

Boolean indicates a logical entity. It has only two values, true and false, respectively.

The Boolean packaging type is the reference type corresponding to the Boolean value. Using a Boolean object in a Boolean expression may 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 scenario

Boolean type is mainly used in the following scenarios:

【1】 Conditional and looping statements

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

if (a> 1) {
// When the condition is true, execute here
} else {
// When the condition is false, execute here
}
【2】 Logical operators

Logical operator is also called Boolean operator. The logical negation operator always returns a boolean value, which is not the case with logical OR and logical AND operations

At the same time use a logical not operator, you can convert the type to Boolean

console.log (!! 1); // true
console.log (!! 0); // false
console.log (!! ''); // true
console.log (!! ''); // false
【3】 Relational operators

The relational operator is used to test the relationship between two values, and returns true or false according to whether the relationship exists. The relational expression always returns a boolean value. Usually, the relational expression is used in if, while or for statements to Control program execution flow

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

Convert a value to a Boolean value can use Boolean () transformation function

False value

The value converted into false is called a false value (falsy value), and these 7 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 empty and blank strings 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 7 false values, all other values converted to Boolean values are true, also known as true value (truthy value)

[Note] The conversion result of all objects (including empty objects) is true, even the Boolean object corresponding to false new Boolean (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

The Boolean object is a wrapper type corresponding to the Boolean value, inheriting the three methods of the Object object's general methods toString (), toLocaleString (), valueOf ()

[ToString ()]

ToString () method returns Boolean string value ('true' or 'false')

[ToLocaleString ()]

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 detailed content of parsing the Boolean Boolean type in the JavaScript type. For more, please pay attention to the first PHP community. Other related articles!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.