Javascript type conversion USES methods

  • 2020-03-30 01:37:56
  • OfStack

(1) convert to a string


X + ''
toString()
String()

Converting a function to a string typically outputs the body of the function, but you can override its toString method

(2) convert to Numbers


parseInt(),parseFloat()

Use Number(), no new in front, and you'll be converted to an object.

Participate in mathematical operations (except addition)

+ X: notice this is not an addition

(3) convert to Boolean value

Automatic conversion: if statement, ||, &&,! The expression is automatically converted to a Boolean value in the logical judgment environment

Cast: use!! , using a Boolean(), no new, that converts to an object.

(iv) conversion to objects

Direct new+ object name

(v) summary

Each type has: the object name (the variable to be converted) conversion method, as well as some techniques that each has independently.

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

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201402/20140208160641.jpg? 20141816714 ">

Related articles: