Detailed introduction to operator rules and implicit type conversions in JavaScript
巴扎黑
Release: 2017-09-07 11:51:14
Original
1248 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.
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:
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
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 introduction to operator rules and implicit type conversions 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