preface

Cast: Casts a data type to another data type.

Type conversion refers to converting other data types to String, Number, and Boolean. Would you convert a data type to null or undefined? No, because there’s no point in doing that.

Other simple types –> String

Method 1: Variable +”” or variable +” ABC”

Examples are as follows:

vat a = 123;  / / Number type
console.log(a + ' ');  // Convert to String
console.log(a + 'haha');  // Convert to String
Copy the code

In the example above, the printed result is all string data.

Method two: Call the toString() method

Examples are as follows:

Variables. The toString ()Copy the code

【 Important 】 This method does not affect the original variable, it returns the result of the transformation. We could also write a = a.tostring (), in which case we would modify the original variable directly.

Note that null and undefined do not have a toString() method, so they cannot be used with method two. If called, an error is reported.

Alternatively, variables of type Number can pass an integer as an argument in the method when toString() is called. At this point, it converts the number to the specified base, or defaults to base 10 if not specified. Such as:

        var a = 255;

        // toString() for Number can be passed an integer as an argument in the method
        // At this point it will convert the number to the specified base, if not specified, the default conversion to base 10
        a = a.toString(2);

        console.log(a);        / / 11111111
        console.log(typeof a); // string
Copy the code

Method three: Use the String() function

The format is as follows:

String (variable)Copy the code

When using the String() function to cast:

  • For Number and Boolean, this is essentially a call to the toString() method.

  • But for null and undefined, the toString() method is not called. It converts null directly to “NULL”. Converts undefined directly to “undefined”.

Prompt () : user input

As we explained in the first JS Basics article, prompt() is specifically used to bring up dialog boxes for user input. The important thing is that whatever the user enters is a string.

Other data types –> Number

Method 1: Use the Number() function

Case one: string –> number

  • 1. If the string contains pure numbers, it is directly converted to numbers.

  • 2. If there is non-numeric content in the string, it is converted to NaN. (You can see the limitations of the Number() function here.)

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

Case 2: Boolean –> numbers

  • True to 1

  • False to 0

Case 3: NULL –> number

  • The result is: 0

Case 4: undefined –> number

  • The result is NaN

Method 2:parseInt(): string -> integer

ParseInt () is used specifically for strings.

ParseInt () converts the valid integer content of a string to a number. Parse stands for “convert” and Int stands for “integer” (note the spelling of Int). Such as:

	parseInt("5");
Copy the code

The result is the number 5.

ParseInt () also has the following properties:

(1) Only the first digit of the string is reserved, and the Chinese characters after it disappear automatically. Such as:

Console. log(parseInt(" I wrote 6 articles in 2017 ")); // Print the result: 2017 console.log(parseInt("2017.01 wrote 6 articles on the public account ")); Console. log(parseInt(" AAa2017.01 wrote 6 articles on the public account ")); // Prints the result: NaNCopy the code

(2) Automatically truncated decimal function: rounded, not rounded.

Case 1:

	var a = parseInt(5.8) + parseInt(4.7);
	console.log(a);
Copy the code

Console output:

	9
Copy the code

Example 2:

	var a = parseInt(5.8 + 4.7);
	console.log(a);
Copy the code

Console output:

	10
Copy the code

(3) If you use parseInt() or parseFloat() for a non-string, it will convert it to String first and then operate on it.

Such as:

    var a = true;
    console.log(parseInt(a));  // Prints the result: NaN (because a is converted to the string "true" first, and then)

    var b = null;
    console.log(parseInt(b));  // Print the result: NaN (because b is converted to the string "null", and then the operation)

    var c = undefined;
    console.log(parseInt(c));  // Print the result: NaN (because b is converted to the string "undefined", and then the operation)

    var d = 168.23;
    console.log(parseInt(d));  // Print the result: 168 (because c is converted to string "168.23", and then the operation)

Copy the code

(4) With two parameters, it represents the base conversion.

parseFloat(): string –> floating point (decimal)

ParseFloat () is designed specifically for strings.

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

ParseFloat () and parseInt() work similarly, except that parseFloat() can get a valid decimal part.

Code examples:

    var a = '123.456.789 px';
    console.log(parseFloat(a)); // Print the result: 123.456
Copy the code

Converted to Boolean

To convert other data types to Boolean, use the Boolean() function.

  • Case one: numbers –> Boolean. All but 0 and NaN are true.

  • Case two: string –> Boolean. All but empty strings are true.

  • Case 3: Null and undefined are both converted to false.

  • Case 4: Objects are also converted to true.

PS: This is the case above, very important, development will use a lot.

Other base numbers

  • A hexadecimal number starting with 0x

  • A base 8 number starting with 0

  • Base 2 numbers, starting with 0b (not supported on all browsers: Chrome and Firefox support it, Not IE)

For example, the string 070, if I call parseInt() to convert it to a number, some browsers will parse it as base 8, some as base 10.

So, the comparison suggests that you pass a second argument in parseInt() to specify the base of the number. Such as:

	a = "070";

	a = parseInt(a,10); // Convert to decimal
Copy the code