Home  >  Article  >  Web Front-end  >  How to use javascript type conversion_Basic knowledge

How to use javascript type conversion_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:01:071015browse

(1) Convert to string

Copy code The code is as follows:

X ' '
toString()
String()

Converting a function to a string usually involves outputting the function body, but you can override its toString method

(2) Convert to numbers

Copy code The code is as follows:

parseInt() ,parseFloat()

Use Number() without adding new in front, so it will be converted into an object.

Participate in mathematical operations (except addition)

X: Note that this is not addition

(3) Convert to Boolean value

Automatic conversion: If statements, ||, &&, ! and other logical judgment environments will automatically convert expressions into Boolean values ​​

Force conversion: use!!, use Boolean(), do not add new in front, so it will be converted into an object.

(4) Convert to object

Direct new object name

(5) Summary

Each type has: object name (variable to be converted) conversion method, as well as some independent skills.

JS type conversion, you can refer to the figure below:

How to use javascript type conversion_Basic knowledge
Statement:
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