Parsing Boolean types in JavaScript types
Previous words
The Boolean value Boolean type may be three types of packaging objects Number, String and Boolean The simplest one. Number and String objects have a large number of instance properties and methods , but Boolean has very few. In a sense, programming a computer is about working with Boolean values, and as a fundamental fact, all electronic circuits can only recognize and use Boolean data. This article will introduce the Boolean type
Definition
The Boolean type represents a logical entity. It has only two values, the reserved words true and false, which represent true and false respectively. Two states
The Boolean packaging type is the reference type corresponding to the Boolean value. Using Boolean objects in Boolean expressions can easily cause misunderstandings
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 type is mainly used in the following scenarios:
【1】Conditions and Loop Statement
Boolean values are mainly used in conditional and conditional parts of loop statements. For example, in the if statement, if the Boolean value is true, the first piece of logic is executed, and if it is false, the other piece of logic is executed. Usually a comparison that creates a Boolean value is combined directly with the statement that uses the comparison
if(a > 1){ //条件为true时,执行此处 }else{ //条件为false时,执行此处 }
【2】logical operator
The logical operator is It's called a Boolean operator. Logical NOT operator always returns a Boolean value, but this is not the case for logical OR and logical AND operations
Using a logical NOT operator at the same time, you can Type conversion to Boolean type
console.log(!!1);//true console.log(!!0);//false console.log(!!' ');//true console.log(!!'');//false
【3】Relational operator
Relational operator is used to test the relationship between two values and returns based on whether the relationship exists True or false, relational expressions always return a Boolean value. Relational expressions are usually used in if, while or for statements to control the execution flow of the 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 Boolean() transformationFunction
False value
The value converted to false is called a falsy value. These seven values include undefined, null, +0, -0, NaN, false, ""(emptystring)
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 strings and blank strings are converted to 0, while in the Boolean method, the empty string "" is converted to false, and the blank characters Convert string " " to true
console.log(Number(''));//0 console.log(Number(' '));//0 console.log(Boolean(''));//false console.log(Boolean(' '));//true
Except for these 7 false values, all other values converted to Boolean values are true, also called truthy values
[Note] The conversion results of all objects (including empty objects) are true, even the Boolean object new Boolean(false) corresponding to 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
Boolean The object is a packaging type corresponding to a Boolean value. Inherites the three general methods toString(), toLocaleString(), and valueOf() of the Object object
【toString()】
The toString() method returns a Boolean string value ('true' or 'false')
【toLocaleString()】
toLocaleString() The 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 Boolean types in JavaScript types. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples. 1. Basic syntax 1.1 Comments In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments. //This is a single-line comment/*This is a multi-line comment*/1.2 Keyword C language

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python
