This is the 7th day of my participation in the August More Text Challenge

An overview of the

There are six simple data types in JS: undefined, NULL, Boolean, string, number, symbol, and reference type: Object

But we only have one data type when we declare it, and the current type is not determined until runtime

let x = y ? 1 : a;
Copy the code

In the above code, the value of x is not available at compile time and is not known until the program runs

Although the data type of a variable is uncertain, the various operators have requirements on the data type, and if the operator does not match the expected type, the type conversion mechanism will be triggered

Common type conversions are:

  • Cast (explicit cast)

  • Automatic conversion (implicit conversion)

Explicit conversion

Explicit conversions, where it is clear that a type shift has occurred, are common methods:

  • Number()

  • parseInt()

  • String()

  • Boolean()

Number()

Converts a value of any type to a numeric value

Let’s start with the type conversion rule:

Practice:

Number(324/ / 324
Copy the code

String: If it can be parsed to a value, it is converted to the corresponding value

Number('324'/ / 324
Copy the code

String: Returns NaN if it cannot be parsed as a number

Number('324abc'// NaN
Copy the code

An empty string is converted to 0

Number(' '/ / 0
Copy the code

Boolean values: true to 1, false to 0

Number(true/ / 1

Number(false/ / 0
Copy the code

Undefined: converts to NaN

Number(undefined// NaN
Copy the code

Null: The value is changed to 0

Number(null/ / 0
Copy the code

Objects: Usually converted to NaN(except arrays containing only a single value)

Number({a1}) // NaN

Number([1.2.3]) // NaN

Number([5]) / / 5
Copy the code

As you can see from the above, Number is strictly converted; if one character cannot be converted to a numeric value, the entire string is converted to NaN

parseInt()

ParseInt is less strict than Number. ParseInt parses characters one by one and stops when it finds one that cannot be converted

parseInt('32a3'/ / 32
Copy the code

String()

You can convert any type of value to a string

The transformation rule diagram is given:

Practice:

Value: Converts to the corresponding string

String(1/ / "1"
Copy the code

String: the original value after conversion

String("a"// "a"
Copy the code

Boolean: true to string “true”, false to string “false”

String(true// "true"

//undefined: converts to string "undefined"

String(undefined// "undefined"

//null: converts to string "null"

String(null// "null"
Copy the code

object


String({a1}) // "[object Object]"

String([1.2.3]) / / "1, 2, 3"
Copy the code

Boolean()

A value of any type can be converted to a Boolean as follows:

Practice:

Boolean(undefined// false

Boolean(null// false

Boolean(0// false

Boolean(NaN// false

Boolean(' '// false

Boolean({}) // true

Boolean([]) // true

Boolean(new Boolean(false)) // true
Copy the code

Implicit conversion

In implicit conversions, perhaps the biggest question we have is: When does an implicit conversion occur?

Here we can summarize the scenarios of implicit conversion in two cases:

  • Comparison operations (==,! =, >, <), if, while where Boolean values are required

  • Arithmetic operations (+, -, *, /, %)

In addition to the above scenario, it is required that the operands on both sides of the operator are not of the same type

Automatically converts to a Boolean value

Where Boolean values are required, non-Booleans are automatically converted to Booleans, and Boolean functions are called internally

Here’s a summary:

  • undefined

  • null

  • false

  • + 0

  • 0

  • NaN

  • “”

All but the ones above are converted to false, and all are converted to true

Automatically converts to a string

Where a string is expected, a non-string value is automatically converted to a string

The rule is to convert a value of the compound type to a value of the primitive type, and then convert the primitive type to a string

Occurs in the + operation, where string concatenation occurs once a string is present

'5' + 1 / / '51'

'5' + true // "5true"

'5' + false // "5false"

'5' + {} // "5[object Object]"

'5' + [] / / "5"

'5' + function (){} // "5function (){}"

'5' + undefined // "5undefined"

'5' + null // "5null"
Copy the code

Automatically converts to a value

Except for +, which may convert an operator to a string, all operators automatically convert an operator to a number

'5' - '2' / / 3

'5' * '2' / / 10

true - 1  / / 0

false - 1 // -1

'1' - 1   / / 0

'5' * []    / / 0

false / '5' / / 0

'abc' - 1   // NaN

null + 1 / / 1

undefined + 1 // NaN

Copy the code

When null is converted to a value, the value is 0. When undefined is converted to a value, the value is NaN