Cast (explicit cast)

A cast is used to manually convert a value of various types to a Number, String, or Boolean value using the three functions Number(), String(), and Boolean().

1. Number()

Number(true) // 1 Number(false) // 0 // undefined: // NaN // null: 0 Number(null) // 0Copy the code
  • It’s stricter than parseInt. If one character cannot be converted to a value, the entire string is converted to NaN:

    parseInt(’42 cats’) // 42

    Number(’42 cats’) // NaN

  • The Number method returns NaN when the argument is an object, except for an array containing a single value:

    Number({a: 1}) // NaN Number([1, 2, 3]) // NaN Number([5]) // 5

  • In addition to Number(), parseInt() and parseFloat() can be converted. The difference is that parseInt() and parseFloat() can only be called on strings. And Number() is not limited to strings

2. String()

The String method returns a type String if the argument is an object. If it is an array, return the array as a string:

A: String ({1}) / / "[object object]" String ([1, 2, 3]) / / "1, 2, 3"Copy the code

3. Boolean()

All values are true except for the following five values that convert to false.

  • undefined

  • null

  • 0 (including -0 and +0)

  • NaN

  • “(empty string)

Two, automatic conversion (implicit conversion)

On the basis of a cast, the conversion function of what type of value is expected is called. For example, if a location is expected to be a String, the String() function is called to convert. If the position can be either a string or a value, it defaults to a value.

1. Subtraction, multiplication and division

Applying the mathematical operator (- * /) to various non-number types first converts non-number types to Number types:

1-1-1 - null // 1 NaN 2 * ['5'] // 10, ['5'] will first become '5', and then the number 5Copy the code

2. The particularity of addition

Since + can also be used to concatenate strings in JS, remember three things:

  • When a side forStringType that is recognized as string concatenation and will preferentially convert the other side to string type.
  • When a side forNumberType, the other side is the original type, the original type is converted toNumberType.
  • When a side forNumberType, on the other side is the reference type, which references the type andNumberType to string concatenation.

The above three points are prioritized from high to low, such as:

123 + '123' // "123123" (Rule 1) 123 + null // 123 (Rule 2) 123 + {} // "1[object object]" (Rule 3)Copy the code

3. Unary operators also convert operators to numbers:

+'abc' // NaN
-'abc' // NaN
+true // 1
-false // 0
Copy the code

3. Use the 5 rules of == comparison

NaN always returns false when compared to any other type (including itself)

2. Boolean is converted to Number first compared to any other type:

True == 1 // true true == '2' // false, instead of '2' to trueCopy the code

String = Number; String = Number;

123 == '123' // true, '123' will become 123 first == 0 // true, '123' will become 0 firstCopy the code

Null == undefined; otherwise, null, undefined, and any other comparison value is false.

5. When comparing a primitive type with a reference type, the reference type is converted to the original type according to ToPrimitive rules. If a primitive type is still not returned, TypeError is raised.

The ToPrimitive rule, which converts a reference type to a primitive type, follows the valueOf then toString pattern of expecting a primitive type.

'[object object]' == {} // true ToString == [1, 2,3]; toString == [1, 2,3]Copy the code

The resources