Home >
Web Front-end >
JS Tutorial >
Detailed explanation of operator rules and implicit type conversion examples in JavaScript
Detailed explanation of operator rules and implicit type conversion examples in JavaScript
韦小宝
Release: 2018-01-25 10:58:46
Original
1542 people have browsed it
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:
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
Copy after login
Finally, we can get the following JavaScript primitive type conversion table (including examples of conversion from composite types to primitive types):
obj = {
valueOf: function () {
console.log("valueOf");
return {}; // not a primitive
},
toString: function () {
console.log("toString");
return {}; // not a primitive
}
}
obj + 1
// error
Uncaught TypeError: Cannot convert object to primitive value
at <anonymous>:1:5
Copy after login
值得一提的是对于数值类型的 valueOf() 函数的调用结果仍为数组,因此数组类型的隐式类型转换结果是字符串。而在 ES6 中引入 Symbol 类型之后,JavaScript 会优先调用对象的 [Symbol.toPrimitive] 方法来将该对象转化为原始类型,那么方法的调用顺序就变为了:
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!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn