Description of Boolean Type in JavaScript systems, javascriptboolean

Source: Internet
Author: User

Description of Boolean Type in JavaScript systems, javascriptboolean

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 scenarios

Boolean is mainly used in the following scenarios:

[1] condition and loop statement

Boolean values are mainly used in the condition section of the condition and loop statement. For example, in an if statement, if the Boolean value is true, the first logic is executed. if the Boolean value is false, another logic is executed. Usually, a comparison to create 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 operators are also called boolean operators. Logical non-operators always return boolean values, but not logical or logical and operation.

At the same time, a logical non-operator can be used to convert a type to a boolean type.

console.log(!!1);//true
console.log(!!0);//false
console.log(!!' ');//true
console.log(!!'');//false

[3] Relational operators

Relational operators are used to test the relationship between two values. true or false is returned based on whether the relationship exists. The relational expression always returns a Boolean value, relational expressions are usually used in if, while, or for statements to control the execution process of a program.

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

Convert to boolean

To convert a value to a Boolean value, you can use the Boolean () transformation function.

False Value

The value converted to false is called a false value. These seven values include undefined, null, + 0,-0, NaN, false, and "" (null 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 the seven dummy values, all other values converted to boolean values are true, also known as truthy value)

[Note] the Conversion Result of 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(new Boolean(false)));//true
console.log(Boolean(false));//false
console.log(Boolean(new Boolean(null)));//true
console.log(Boolean(null));//false

Instance method

A Boolean Object is a packaging type corresponding to a Boolean value. It inherits the following methods: toString (), toLocaleString (), and valueOf ().

[ToString ()]

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

[ToLocaleString ()]

The toLocaleString () method returns the 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());//'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 a full explanation of the Boolean Type of the JavaScript type system described in the small Editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in time!


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.