Home > Article > Web Front-end > Detailed introduction to operator rules and implicit type conversions in JavaScript
What are the implicit type conversions of operator rules in JavaScript? This is a question that every novice learning JavaScript should know. The following article mainly introduces you to the operator rules and implicit type conversions in JavaScript. Friends who need it can refer to the relevant information on type conversion. Let’s take a look below.
Preface
This article mainly introduces relevant content about JavaScript operator rules and implicit type conversion, and shares it for your reference. Learning, I won’t say much more below, let’s take a look at the detailed introduction.
Implicit type conversion
In JavaScript, when we perform comparison operations or the four arithmetic operations of addition, subtraction, multiplication and division, we often Will trigger JavaScript's implicit type conversion mechanism; this part is often confusing. For example, the console.log operation in the browser often converts any value into a string and then displays it, while mathematical operations first convert the value into a numeric type (except Date type objects) and then perform operations.
Let’s first look at several sets of typical operator operation results in JavaScript. We hope that after reading this section, we can have a reasonable explanation for each item:
// 比较 [] == ![] // true NaN !== NaN // true 1 == true // true 2 == true // false "2" == true // flase null > 0 // false null < 0 // false null == 0 // false null >= 0 // true // 加法 true + 1 // 1 undefined + 1 // NaN let obj = {}; {} + 1 // 1,这里的 {} 被当成了代码块 { 1 + 1 } + 1 // 1 obj + 1 // [object Object]1 {} + {} // Chrome 上显示 "[object Object][object Object]",Firefox 显示 NaN [] + {} // [object Object] [] + a // [object Object] + [] // 等价于 + "" => 0 {} + [] // 0 a + [] // [object Object] [2,3] + [1,2] // '2,31,2' [2] + 1 // '21' [2] + (-1) // "2-1" // 减法或其他操作,无法进行字符串连接,因此在错误的字符串格式下返回 NaN [2] - 1 // 1 [2,3] - 1 // NaN {} - 1 // -1
Conversion between primitive types
The primitive types we often talk about in JavaScript include numeric types, string types, Boolean types and null There are several types; and the conversion functions between our commonly used primitive types are String, Number and Boolean:
// String let value = true; console.log(typeof value); // boolean value = String(value); // now value is a string "true" console.log(typeof value); // string // Number let str = "123"; console.log(typeof str); // string let num = Number(str); // becomes a number 123 console.log(typeof num); // number let age = Number("an arbitrary string instead of a number"); console.log(age); // NaN, conversion failed // Boolean console.log( Boolean(1) ); // true console.log( Boolean(0) ); // false console.log( Boolean("hello") ); // true console.log( Boolean("") ); // false
Finally, we can get the following JavaScript primitive type conversion Table (including examples of conversion from composite types to primitive types):
Primary value | Convert to numeric type | Convert to string Type | is converted to Boolean type |
---|---|---|---|
false | 0 | "false" | false |
true | 1 | "true" | true |
0 | 0 | "0" | false |
1 | "1" | true | |
0 | "0" | true | |
1 | "1" | true | |
NaN | "NaN" | false | |
Infinity | " Infinity" | true | ##-Infinity |
"-Infinity" | true | "" | |
"" | false | "20" | |
"20" | true | ##"twenty" | |
"twenty" | true | [ ] | |
"" | true | [20] | |
"20" | true | [10,20 ] | |
"10,20" | true | ["twenty"] | |
"twenty" | true | ##["ten","twenty"] | NaN |
true | function(){} | NaN | |
true | { } | NaN | |
true | null | 0 | |
false | undefined | NaN | |
false |
The above is the detailed content of Detailed introduction to operator rules and implicit type conversions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!