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

Type Conversion in Javascript

Linda Hamilton
Release: 2024-11-09 05:47:02
Original
477 people have browsed it

Type Conversion in Javascript

Converting to string

String()

It can take null and undefined as arguments without error.

String(null); // 'null'
String(undefined); // 'undefined'
Copy after login

toString()

It throws an error if the variable is set to null or undefined.

null.toString() // Uncaught TypeError: Cannot read properties of null (reading 'toString')
undefined.toString() // Uncaught TypeError: Cannot read properties of null (reading 'toString')
Copy after login

Converting to Number

Number()

This constructor converts an argument to a number.
If the characters in the argument can't be interpreted as a number, it converts the argument to NaN

Number('123px'); // NaN
Number('3'); // NaN
Copy after login

parseInt() or parseFloat()

It reads the number until it reaches the character that can't be interpreted as a number.

parseInt('12px'); // 12
parseint(''); // NaN
Copy after login

The above is the detailed content of Type Conversion in Javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template