Home Web Front-end JS Tutorial Detailed introduction to the differences between Javascript Boolean, Nnumber, and String forced type conversion_Basic knowledge

Detailed introduction to the differences between Javascript Boolean, Nnumber, and String forced type conversion_Basic knowledge

May 16, 2016 pm 05:46 PM
cast type conversion

Let’s talk in detail about the differences between Boolean, Nnumber, and String casts in Javascript.
We know that Boolean(value) converts the value into a Boolean type, Nnumber(value) converts the value into a number (integer or floating point number), and String(value) converts the value into a string.

Let’s analyze Boolean first. Boolean returns true when the conversion value is “a string with at least one character”, “non-zero number” or “object”; when the conversion value is “empty string” ", "Number 0", "undefined", "null" returns false.
For example:

Copy code The code is as follows:

var b1 = Boolean("" );//Return false, empty string
var b2 = Boolean("s");//Return true, non-empty string
var b3 = Boolean(0);//Return false, number 0
var b4 = Boolean(1);//Return true, non-0 number
var b5 = Boolean(-1);//Return true, non-0 number
var b6 = Boolean(null); //Return false
var b7 = Boolean(undefined); //Return false
var b8 = Boolean(new Object()); //Return true, object

Next Analyze Number. Number is similar to parseInt and parseFloat. The difference is that Number converts the entire value, while parseInt and parseFloat can only convert the beginning number part.
For example:
Number(“1.2.3″), Number(“123abc”) will return NaN, while parseInt(“1.2.3″) returns 1, parseInt(“123abc”) returns 123, parseFloat( "1.2.3") returns 1.2, ParseFloat("123abc") returns 123.
Number will first determine whether the value to be converted can be completely converted, and then determine whether to call parseInt or parseFloat.
The results of calling Number are listed below:
Copy the code The code is as follows:

Number(false) //Return 0
Number(true) //Return 1
Number(undefined) //Return NaN
Number(null) //Return 0
Number("1.2 ") //Return 1.2
Number("12") //Return 12
Number("1.2.3") //Return NaN
Number(new Object()) //Return NaN
Number(123) //Return 123

Finally, let’s analyze String. String can convert all types of data into strings. For example: the result of String(false) is "false", String( The result of 1) is "1". It is somewhat different from the toString method. The difference is as follows:
Copy code The code is as follows:

var s1 = null;
var s2 = String(t1);//The value of s2 is "null"
var s3 = s1.toString();//An error will be reported
var s4;
var s5 = String(t4);//The value of s5 is "undefined"
var s6 = t4.toString();//An error will be reported
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert string to int type in go language How to convert string to int type in go language Mar 15, 2021 pm 02:00 PM

Conversion method: 1. Use the Atoi() function in the strconv package to convert the string type integer to the int type, with the syntax "strconv.Atoi(string)"; 2. Use the ParseInt() function in the strconv package to convert Returns an integer value represented by a string (accepts sign), syntax "strconv.ParseInt(string,10,64)".

How to solve C++ runtime error: 'invalid type conversion'? How to solve C++ runtime error: 'invalid type conversion'? Aug 27, 2023 pm 03:33 PM

How to solve C++ runtime error: 'invalidtypeconversion'? During the C++ programming process, we often encounter various compile-time and run-time errors. One of the common runtime errors is the 'invalidtypeconversion' error. This error is triggered when we convert one data type to another incompatible data type. This article will introduce some common causes of this error and how to solve it.

C++ compilation error: Invalid type conversion, how to deal with it? C++ compilation error: Invalid type conversion, how to deal with it? Aug 22, 2023 am 10:55 AM

As a strongly typed language, C++ requires special attention when converting data types, otherwise the compiler will report an error. One of the more common errors is "invalid type conversion". This article will explain why this error occurs, how to perform type conversion, and how to avoid this error. 1. Cause of the error: Data type mismatch. There are some data types in C++ that cannot be converted directly. For example, you cannot convert a character variable directly to an integer variable, or a floating-point variable directly to a Boolean variable.

Type conversion of golang function Type conversion of golang function Apr 19, 2024 pm 05:33 PM

In-function type conversion allows data of one type to be converted to another type, thereby extending the functionality of the function. Use syntax: type_name:=variable.(type). For example, you can use the strconv.Atoi function to convert a string to a number and handle errors if the conversion fails.

Implicit type conversion: An exploration of different variations of types and their applications in programming Implicit type conversion: An exploration of different variations of types and their applications in programming Jan 13, 2024 pm 02:54 PM

Explore the different types of implicit type conversions and their role in programming Introduction: In programming, we often need to deal with different types of data. Sometimes, we need to convert one data type to another type in order to perform a specific operation or meet specific requirements. In this process, implicit type conversion is a very important concept. Implicit type conversion refers to the process in which the programming language automatically performs data type conversion without explicitly specifying the conversion type. This article will explore the different types of implicit type conversions and their role in programming,

Type conversion problem in Java - how to solve java.lang.NumberFormatException? Type conversion problem in Java - how to solve java.lang.NumberFormatException? Jun 25, 2023 am 10:54 AM

In Java development, we often encounter type conversion problems. When we convert a value of one data type to a value of another data type, if the conversion is incorrect, a java.lang.NumberFormatException exception will be thrown. This article will describe the cause of this exception and how to avoid it. java.lang.NumberFormatException exception reason java.lang.NumberFormatExcep

Casting of PHP function parameters Casting of PHP function parameters Apr 19, 2024 pm 02:51 PM

The PHP function parameter casting feature allows parameters to be converted to specific data types to ensure correct data entry. Forced conversion syntax: functionfunc(mixed$param):type{...}, where mixed means that any type of data can be accepted, and type means the expected type. PHP supports coercion of parameters into int, float, string, bool and array types. Coercion will not modify the original parameter value. Casting is useful when strict type checking is required.

How to convert int to string type in go language How to convert int to string type in go language Jun 04, 2021 pm 03:56 PM

Conversion method: 1. Use the Itoa() function, the syntax "strconv.Itoa(num)"; 2. Use the FormatInt() function to convert int type data into the specified base and return it in the form of a string, the syntax "strconv .FormatInt(num,10)".

See all articles