Javascript type conversion USES methods

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

(1) convert to a string

X + ''

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

(2) convert to Numbers


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 / / 20141816714 ">

Related articles: