Cast casting

Casting refers to casting a data type to another data type. Casting mainly refers to casting other data types to

Number String Boolean

1. Convert other data types to strings

A:

Call the toString() method of the converted type. This method does not affect the original variable and returns the result of the conversion,

Note, however, that null and undefined do not have toString() methods and will return an error if their methods are called out of place

Method 2:

Call the String() function and pass the converted data as an argument to the function

The toString() method is called for Number and Boolean, but for Null and undefined, toString() is not called:

It will convert NULL directly to “null” and it will convert unas directly to “undefined”

2. Convert other data types to Number

Conversion mode 1:

Use the Number() function

– String –> number

1. If the string is purely numeric, it is directly converted to a number

2. If there is non-numeric content in the string, it is converted to NaN

3. If the string is an empty string or a string full of Spaces, it is converted to 0

Boolean –> numbers

True to 1

False to 0

Null — — > 0

Undefined — — > digital NaN

Conversion mode 2:

This is used specifically for strings

ParseInt () converts a string to an integer

ParseFloat () converts a string to a floating point number (decimal)

3. Convert other data types to Boolean

Use the Boolean() function

Numbers –> Boolean all but 0 and NaN are true

String –> Boolean all but empty strings are true

Nullheundefined will be converted to false

The object is also converted to true