JavaScript often needs to perform operations on data, such as casting data types or common addition, subtraction, multiplication and division operations. There are two main types of data types involved: cast and automatic cast

Cast casting

Cast casting uses the Number, String, and Boolean functions to manually convert various data types to numbers, strings, and Booleans, respectively

var a = '123' // string
var b = Number(a) // number
var c = Boolean(a) // boolean
Copy the code
Other data types are converted to strings
    1. The toString() method // array, function, and primitive types all overwrite toString
    1. String () method
The other data types are converted to Number
    1. Use the Number() function
    1. Use parseInt() and parseFloat()
Other data types are converted to Boolean

Its conversion rules are relatively simple, only empty strings, undefined, null, +0, -0, NaN is converted to false, and everything else is converted to true

Boolean('') // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(0) // false
Boolean(NaN) // false
Copy the code

Empty arrays and empty objects also return true, and even Boolean objects corresponding to false return true

Boolean({}) // true
Boolean([]) // true
Boolean(new Boolean(false)) // true
Copy the code
Automatic type conversion

In the following cases, JavaScript automatically converts data types that are not visible to the user

  1. Automatically converts to a Boolean value

var a = ‘123’ if (a) {}

Javascript converts a non-Boolean value to a Boolean type when it encounters an expected Boolean value (such as an if conditional statement), and the system automatically calls the Boolean function

  1. Automatically converts to a value

The arithmetic operator (+-*/), when operating on a non-number value, automatically converts the value to a Number, except for string addition (which concatenates the value to a string as long as it includes a string).

'123' + 345 // 123345
true + 1 // 2
[] + 1 // 1
1 * '6' // 6
Copy the code
  1. Automatically converts to a string

Automatic conversion of strings occurs mainly during addition operations. Non-string types are converted to string types and then added. There are two main types

The addition operator automatically converts a value of a reference type to string addition if there is at least one of the two string types

True + '1' / / "true1" [] + '3' / / "3 '5' +" function () {} / / "5 function () {} {} + 2 / /" [object object] 2 "conversion example Number (' 123 ') / / 123 Number (' 123 ABC ') / / NaN Number ({}) / / NaN Number ([]) / / 0 Number ([1, 2]) / / NaN Number ([1]), / / 1 Number(function(){}) // NaN String(123) // '123' // call toString() String(true) // 'true' // on the numeric wrapper object type Call toString() String({}) // '[object object]' // Call toString() on the object prototype. // "// Call toString() on the array prototype String(function(){}) // 'function(){}' call toString() method Boolean('') // false Boolean(undefined) // false Boolean(null) // false Boolean(NaN) // false Boolean(0) // false 1+[] // '1' 1+{} // '1[object Object]' 1 + [2] // '12' 1 + function(){} // '1function(){}' 1 + true // 2 2 - [2] // 0 2 -{} // NaNCopy the code