Home >Web Front-end >JS Tutorial >Detailed explanation of operator rules and implicit type conversion examples 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.
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 empty types; and we commonly use The conversion functions between 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):
Original value | Convert to numeric type | Convert to string type | Convert to Boolean type |
---|---|---|---|
false | 0 | "false" | false |
true | 1 | "true" | true |
0 | 0 | "0" | false |
1 | 1 | "1" | true |
"0" | 0 | "0" | true |
"1" | 1 | "1" | true |
NaN | "NaN" | false | |
Infinity | "Infinity" | true | |
-Infinity | "-Infinity" | true | |
0 | "" | false | |
20 | "20" | true | |
NaN | "twenty" | true | |
0 | "" | true | |
20 | "20" | true | |
NaN | "10,20 " | true | |
NaN | "twenty" | true | |
NaN | "ten,twenty" | true | |
NaN | "function(){}" | true | ## { } |
"[object Object]" | true | null | |
"null" | false | undefined | |
"undefined" | false |
The above is the detailed content of Detailed explanation of operator rules and implicit type conversion examples in JavaScript. For more information, please follow other related articles on the PHP Chinese website!