Home > Web Front-end > JS Tutorial > body text

Conversion methods between basic data types in JavaScript

php中世界最好的语言
Release: 2020-02-27 14:21:31
forward
2401 people have browsed it

There are six data types in JavaScript, five of which are basic data types and one is a reference data type. The five basic data types are: Number numeric type, String string type, Boolean Boolean type, null type, and undefined undefined type. Let’s start with a detailed introduction to the conversion between them and the points to note during the conversion process.

Conversion methods between basic data types in JavaScript

Convert basic data types to numerical types

1. Convert other data types in JavaScript There are three commonly used methods to convert the four basic data types into numerical types:

1. Through Number (constant/variable)

2. Through parseInt (string that needs to be converted) /parseFloat (string that needs to be converted)

parseInt/parseFloat will extract values ​​from left to right. Once it encounters a non-numeric value, it will stop immediately. If the value is not extracted when it stops, then it will return NaN.

3. Convert through ' ' / '-'

Although other types can be converted into numerical types through /-, - will change the sign of the value; /-The bottom layer Essentially, it calls the Number function

2. Notes on converting the other four basic data types into numerical types:

1. Convert the string type to the numerical type

1. If the strings are all numerical values, then convert them normally

If the string is an empty string, such as "" (nothing is stored) or " " (only spaces are stored) , then it will be 0 after conversion.

If the string contains both numbers and letters, such as "abc123", then it will be NaN after conversion.

2. Convert Boolean type to numeric type

true is converted to 1

false is converted to 0

3. Convert empty type to numeric type

Convert null to 0

4, Convert undefined type to numeric type

Convert undefined to NaN

Basic data Type conversion to string type

1. There are three common methods to convert the other four basic data types into string type in JavaScript:

1. For numeric types and Boolean types, you can use variable name.toString() to convert

2. You can use String (constant/variable);

3. You can also Convert to string through variable/constant ""/''

2. Notes on converting the other four basic data types into string type:

1. Through variables The name.toString() method is to convert the copied data and will not affect the original data

2. You cannot use constants to directly call the toString method because constants cannot be changed

3. String (constant or variable), because a new value is regenerated based on the incoming value, not the original value is modified

4. Pass undefined undefined type through String( ) method to convert to string type, after conversion it is still undefined

5. Convert the null type to string type through String() method, after conversion it is still null

6. Pass Variables/constants ""/'' to convert to a string, the underlying essence is actually to call the String() function

Convert the basic data type to a Boolean type

If you want to convert basic data types to Boolean types in JavaScript, you only need to call Boolean (constant or variable)

Notes on converting the other four basic data types into string types :

1. Convert the string type to a Boolean type. As long as there is content in the string, it will be converted to true. Only if there is no content in the string, it will be converted to false.

2. Convert the numeric type When converting to a Boolean type, only the value 0 will be converted to false. Others will be converted to true. If it is NaN, it will also be converted to false

3. Convert the undefined type to the Boolean type. Undefined will be converted to false

4. Converting null type to Boolean type will be converted to false

5. In JavaScript, NaN belongs to Number type

6. Empty string /0/NaN/undefined/ null will be converted to false, and the others are true

The conversion between data types in JavaScript is similar to that of other programming languages, although it does not separate data types as clearly as C language, such as in JavaScript The numerical types include integers, single-precision/double-precision floating-point types in C, etc.

However, in JavaScript, the conversion between different data types is similar to the conversion between different data types in C language. The conversion types are divided into two types, one is automatic conversion and the other is The first is forced conversion, but there are some new conversion methods in JavaScript.

This article comes from the js tutorial column, welcome to learn!

The above is the detailed content of Conversion methods between basic data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template