How to convert data types in JavaScript

PHPz
Release: 2023-04-23 16:55:58
Original
1053 people have browsed it

JavaScript is a dynamically typed language, which means variables can change their data type at any time while the program is running. Since JavaScript does not require the type of a variable to be explicitly declared in the code, it is necessary to understand how data type conversion is performed in different situations.

JavaScript data types can be divided into primitive types and reference types. Primitive types include numbers, strings, booleans, null, and undefined. Reference types include objects, arrays, and functions.

In JavaScript, you can use some built-in functions to convert one data type to another. These built-in functions include toString(), parseInt(), parseFloat(), and Number().

toString()

The toString() method can convert numbers, strings and Boolean values to string types. For example:

var num = 123; var str = num.toString(); //将数字转换为字符串 console.log(typeof str); //输出: string var bool = true; var str = bool.toString(); //将布尔值转换为字符串 console.log(typeof str); //输出: string
Copy after login

parseInt() and parseFloat()

The parseInt() and parseFloat() methods can convert strings to numeric types. The parseInt() method converts a string to an integer type, while the parseFloat() method converts a string to a floating point type. For example:

var str = "123"; var num = parseInt(str); //将字符串转换为整数 console.log(typeof num); //输出: number var str = "3.14"; var num = parseFloat(str); //将字符串转换为浮点数 console.log(typeof num); //输出: number
Copy after login

Note: If the string cannot be successfully converted to a numeric type, NaN (not a numeric value) will be returned. For example:

var str = "abc"; var num = parseInt(str); //将字符串转换为整数,但是失败 console.log(num); //输出: NaN
Copy after login

Number()

Number() method can convert any data type to a numeric type. For example:

var str = "123"; var num = Number(str); //将字符串转换为数字 console.log(typeof num); //输出: number var bool = true; var num = Number(bool); //将布尔值转换为数字 console.log(typeof num); //输出: number var obj = {a: 1}; var num = Number(obj); //将对象转换为数字,但是失败 console.log(num); //输出: NaN
Copy after login

It should be noted that when converting a string to a number, the Number() method will try to convert the string to an integer or floating point number. If the conversion fails, NaN is returned.

When performing data type conversion, please note that unexpected results may occur. For example, when converting a string to a number, NaN is returned if the string contains non-numeric characters. Therefore, before performing type conversion, the data type should be checked to avoid errors.

The above is the detailed content of How to convert data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!