


Detailed introduction to the differences between Javascript Boolean, Nnumber, and String forced type conversion_Basic knowledge
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:
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:
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:
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

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



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: '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.

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.

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.

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,

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

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.

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)".
