This article brings you what is implicit type conversion? The introduction to js implicit type conversion has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
JavaScript's data types are very weak. When using arithmetic operators, the data types on both sides of the operator can be arbitrary. For example, a string can be added to a number. The reason why operations can be performed between different data types is because the JavaScript engine will quietly perform implicit type conversion on them before the operation. The following is the addition of numeric types and Boolean types:
3 + true; // 结果:4
The result is a numeric value! If it is in a C or Java environment, the above operation will cause an error because the data types on both sides of the operator are inconsistent. However, in JavaScript, only in a few cases, the wrong type will cause an error, such as calling a non-function, or reading When taking null or undefined attributes, it is as follows:
"hello"(1); //结果: error: not a function null.x; // 结果:error: cannot read property 'x' of null
In most cases, JavaScript will not make an error, but will automatically perform the corresponding type conversion. For example, arithmetic operators such as -, *, /, and % will convert the operands into numbers, but the " " sign is a little different. In some cases, it is an arithmetic plus sign, and in other cases, it is a string concatenation. Symbol, the specific details depend on its operands, as follows:
2 + 3; //结果: 5 "hello" + " world"; // 结果:"hello world"
However, if strings and numbers are added, JavaScript will automatically convert the numbers into characters, regardless of whether the numbers or strings come first. As follows:
"2" + 3; // 结果:"23" 2 + "3"; //结果: "23"
The result of adding a string and a number is a string!
It should be noted that the operation direction of " " is from left to right, as follows:
1 + 2 + "3"; // "33"
This is equivalent to the following:
(1 + 2) + "3"; // "33"
Compare Next, the following results are different:
1 + "2" + 3; // "123"
However, implicit type conversion sometimes hides some errors, for example, null will be converted to 0, and undefined will be converted to NaN. It should be noted that NaN and NaN are not equal (this is due to the precision of floating point numbers), as follows:
var x = NaN; x === NaN; // false
Although JavaScript provides isNaN to detect whether a value is NaN, however, This is not very accurate, because before calling the isNaN function, there is an implicit conversion process, which will convert values that are not NaN to NaN, as follows:
isNaN("foo"); // true isNaN(undefined); // true isNaN({}); // true isNaN({ valueOf: "foo" }); // true
The above code , after we used isNaN to test, we found that strings, undefined, and even objects all returned true! ! ! But they are not NaN.
In short: isNaN detection of NaN is not reliable! ! !
There is a reliable and accurate way to detect NaN.
We all know that only NaN is not equal to itself. You can use the inequality sign (!==) to determine whether a number is equal to itself. Therefore, NaN can be detected, as follows:
var a = NaN; a !== a; // true var b = "foo"; b !== b; // false var c = undefined; c !== c; // false var d = {}; d !== d; // false var e = { valueOf: "foo" }; e !== e; // false
We can also define this mode as a function, as follows:
function isReallyNaN(x) { return x !== x; }
Objects can be converted into primitive values , the most common method is to convert it into a string, as follows:
"the Math object: " + Math; // "the Math object: [object Math]" "the JSON object: " + JSON; // "the JSON object: [object JSON]"
The object is converted into a string by calling its toSting function. You can call it manually to check:
Math.toString(); // "[object Math]" JSON.toString(); // "[object JSON]"
Similarly, objects can also be converted into numbers through the value Of function. Of course, you can also customize the value Of function, as follows:
"J" + { toString: function() { return "S"; } }; // "JS" 2 * { valueOf: function() { return 3; } }; // 6
If an object also exists valueOf method and toString method, then the value Of method will always be called first, as follows:
var obj = { toString: function() { return "[object MyObject]"; }, valueOf: function() { return 17; } }; "object: " + obj; // "object: 17"
Generally, try to make the values represented by value Of and toString the same (although the types can be different).
The last type of forced type conversion is often called "truth operation", such as if, ||, &&, their operands are not necessarily Boolean. JavaScript will convert some non-Boolean values into Boolean values through simple conversion rules. Most values will be converted to true, only a few are false, they are : false, 0, -0, "", NaN, null, undefined, because there are numbers, strings and objects The value is false, so it is not very safe to directly use true value conversion to determine whether the parameters of a function are passed in. For example, there is a function that can have optional parameters with default values, as follows:
function point(x, y) { if (!x) { x = 320; } if (!y) { y = 240; } return { x: x, y: y }; }
This function will ignore any parameters whose true value is false, including 0, -0;
point(0, 0); // { x: 320, y: 240 }
A more accurate way to detect undefined is to use the typeof operation:
function point(x, y) { if (typeof x === "undefined") { x = 320; } if (typeof y === "undefined") { y = 240; } return { x: x, y: y }; }
This way of writing can distinguish between 0 and undefined:
point(); // { x: 320, y: 240 } point(0, 0); // { x: 0, y: 0 }
Another method is to use parameters to compare with undefined. As follows:
if (x === undefined) { ... }
Summary:
1. Type errors may be hidden by type conversion.
2. " " can represent both string concatenation and arithmetic addition, depending on its operands. If one of the operands is a string, then it is string concatenation.
3. The object converts itself into a number through the value Of method, and converts itself into a string through the toString method.
4. Objects with value Of methods should define a corresponding toString method to return equal numbers in string form.
5. When detecting some undefined variables, type Of or comparison with undefined should be used instead of true value operation directly.
Related recommendations:
JS implicit type conversion summary
How to use implicit conversion? Summarize the usage of implicit conversion examples
A brief introduction to implicit type conversion of JavaScript data types_javascript skills
The above is the detailed content of What is implicit type conversion? Introduction to js implicit type conversion. For more information, please follow other related articles on the PHP Chinese website!