Cast casting

Basically convert to String, Number,Boolean

1. Other types’ trial trial: string

Method 1: Use the toString() method

Number ➡ String

Methods a

Such as

var  a =123 ; // A numeric type
str_a = a.toString(); // Call toString()
console.log("a=" + a);
console.log("Type of A :" + typeof(a));
console.log("str_a: " + str_a);
console.log("Str_a type:" + typeof(str_a));

/// console output:
a=123Type of A :numberstr_a: 123Str_a is of type stringCopy the code

The toString() method does not affect the original variable and returns a value, so to really change the type, nest another variable (str_A).

Method 2: Call the string() function

1. Null Trial trial

Such as

var  a =null ; / / null type
str_a = String(a); // Call the String() method
console.log("a=" + a);
console.log("Type of A :" + typeof(a));
console.log("str_a: " + str_a);
console.log("Str_a type:" + typeof(str_a));

// console output
a=nullThe type of A is objectstr_a: nullStr_a is of type stringCopy the code

2, undefined trial by default

Such as

var  a =undefined ; // A numeric type
str_a = String(a); // Call toString()
console.log("a=" + a);
console.log("Type of A :" + typeof(a));
console.log("str_a: " + str_a);
console.log("Str_a type:" + typeof(str_a));

// console output
a=undefinedType of A:undefined
str_a: undefinedStr_a is of type stringCopy the code

For Number,Boolean still uses toString ()

2. Other types make number

Method 1: Use the Number() function

var  a ="123" ;
a= Number(a); //type = number
Copy the code

The ⚠️ Number() function N must be capitalized

example

💛1, String Trial

💁♂️ for XXXX Number, the following conditions are available

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

  2. If there are non-numbers in the string, the return value is NaN

    var  a = "123 _45" ; // string A non-numeric character string
    num_a = Number(a); Call the Number() method
     
    console.log("a=" + a);
    console.log("Type of A :" + typeof(a));
    console.log("num_a: " + num_a);
    console.log("Num_a type:" + typeof(num_a));
    
    // console output:
    a=123 _45The type of a is stringnum_a: NaNNum_a is of the number typeCopy the code
  3. If there is a space in the string, the return value is NaN

    var a= "123, 456";
    a = Number(a);
    console.log( a );
    
    // console output:
    NaN
    Copy the code

💛2, Boolean Trial

If the Boolean type converts to a number, true is 1 and false is 0

 var  a = true ; / / a Boolean type
num_a = Number(a); Call the Number() method

console.log("a=" + a);
console.log("Type of A :" + typeof(a));
console.log("num_a: " + num_a);
console.log("Num_a type:" + typeof(num_a));

// console output:
a=trueType of a: Booleannum_a: 1Num_a is of the number typeCopy the code

💛2, null Trial

Null type: the value is 0

var  a = null;
a = Number(a);
console.log(a ); / / 0
Copy the code

4, undefined 💛 ➡ number

Undefined type Trial by digit, NaN

var  a = undefined;
a = Number(a);
console.log(a ); // NaN
Copy the code

💛4. Special methods of type string

ParseInt () : reads a valid integer from a string

var a = "123px";
a = parseInt(a);
console.log("a=" + a); // a=123
console.log("Type of A:" + typeof(a)); // Type of a: number
Copy the code

The parseFloat () : the… The significant decimal is read out

var a = "1200.3 GDGDGPX";
a = parseFloat(a);
console.log("a=" + a); / / a = 1200.3
console.log("Type of A:" + typeof(a)); // Type of a: number
Copy the code

⚠️ There are several cases for reading the integer parseInt() function

  1. a = “123px”;

    //console output:
    123
    number
    Copy the code
  2. a = “a123px”;

    //console output:
    NaN
    number
    Copy the code
  3. a = “123a456”;

    //console output:
    123
    number
    Copy the code

Conclusion:

  • If the first digit is a number, the significant digit portion from the beginning of the number is read

  • NaN if the first digit is not a number

⚠️ There are several cases for reading the integer parseFloat() function

  1. A = “123.456”.

    //console output:
    123.456
    number
    Copy the code
  2. A = “123.456.789”;

    //console output:
    123.456
    number
    Copy the code

Conclusion:

  • ifThe firstIf is a number, read isIn decimalFractional part of
  • ifThe firstIf it’s not a number, take itNaN

For a non-string type, using parseInt() or parseFloat() will change it to a String type and then transform it to

var a  = true;
a  = parseInt(a ) ; // a = parseInt ("true");
console.log( a ); // NaN
console.log( typeof a); // number
Copy the code

(after)