This article describes the implicit type conversion operation of JavaScript. Share it for your reference, as follows:
JavaScript's data types are very weak (or else they won't be called a weak-type language)! 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 different data types can be operated is because the JavaScript engine will quietly convert them implicitly before the operation. The following is the addition of numerical types and boolean types:
3 + true; // 4
The result is a numerical type! If it is in a C or Java environment, the above operation will definitely cause an error due to inconsistent data types on both sides of the operator! However, in JavaScript, there are only a few cases where the error type causes an error.For example, when calling non-functions, or reading null or undefined properties,as follows:
"hello"(1); // error: not a function ; // error: cannot read property 'x' of null
In most cases, JavaScript will not make any errors, but will automatically perform corresponding type conversion. For example, arithmetic operators such as -, *, /, and % will convert operands into numbers, but the "+" sign is a bit different. In some cases, it is an arithmetic plus sign, and in some cases, it is a string concatenation symbol. The specific one depends on its operand.,as follows:
2 + 3; // 5 "hello" + " world"; // "hello world"
But what will happen if strings and numbers are added?JavaScript will automatically convert numbers into characters, regardless of whether the numbers are before or string is before.,as follows:
"2" + 3; // "23" 2 + "3"; // "23"
The result of adding strings and numbers is a string, the result of adding strings and numbers is a string, and the result of adding strings and numbers is a string, say important things three times! ! ! ! ! !
In addition, it should be noted that the operation direction of "+" is from left to right, as follows:
1 + 2 + "3"; // "33"
This is equivalent to:
(1 + 2) + "3"; // "33"
In contrast, the following results are different:
1 + "2" + 3; // "123"
However, implicit type conversion sometimes hides some errors, such asnull will be converted to 0, undefined will be converted to NaN. It should be noted that NaN and NaN are not equal (this is determined by the accuracy of floating point numbers),as follows:
var x = NaN; x === NaN; // false
Although,JavaScript provides isNaN to detect whether a value is NaN, but this is not very accurate, because before calling the isNaN function, there is an implicit conversion process, which converts values that are not originally NaN into NaN.,as follows:
isNaN("foo"); // true isNaN(undefined); // true isNaN({}); // true isNaN({ valueOf: "foo" }); // true
In the above code, after we use isNaN to test, we find that strings, undefined, and even objects, and the result returns true! ! ! But, we can't say that they are also NaNs, right? In short, the conclusion is that isNaN detection is not reliable! ! !
Fortunately, there is a reliable and accurate way to detect NaN. We all know that only NaN is not waiting for itself, so we use the non-equal sign (!==) to determine whether a number is equal to itself, so that 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 pattern as a function, as follows:
function isReallyNaN(x) { return x !== x; }
OK, the detection method of NaN is that simple. Let’s continue to discuss the implicit conversion of objects below!
An object can be converted to a primitive value. 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 and its toSting function is called. You can call it manually to detect it:
(); // "[object Math]" (); // "[object JSON]"
Similarly, objects can also be converted into numbers, and they are passed through the valueOf function. Of course, you can also customize this valueOf function, as follows:
"J" + { toString: function() { return "S"; } }; // "JS" 2 * { valueOf: function() { return 3; } }; // 6
If an object has both a valueOf method and a toString method, then the valueOf method will always be called first, as follows:
var obj = { toString: function() { return "[object MyObject]"; }, valueOf: function() { return 17; } }; "object: " + obj; // "object: 17"
However, in most cases, this is not what we want, in general, make the values represented by valueOf and toString the same (although the types can be different).
The last type of cast, which we often call "true value operation", for example, if, ||, &&, their operands are not necessarily Boolean. JavaScript will convert some non-Boolean values into Boolean types through simple conversion rules. Most values will be converted to true, and only a few are false, they are: false, 0, -0, "", NaN, null, undefined. Because there are numbers, strings and objects, the value of false is false, it is not unsafe to directly use the true value conversion to determine whether a function's parameters have been passed in. For example, there is a function that can have default value optional parameters, 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 with false true values, including 0 and -0;
point(0, 0); // { x: 320, y: 240 }
A more accurate way to detect undefined is to use 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 compare parameters with undefined,as follows:
if (x === undefined) { ... }
Summarize:
1. Type errors may be hidden by type conversion.
2. "+" can represent both string concatenation and arithmetic addition, which depends on its operand. If there is one that is a string, then it is a string concatenation.
3. The object converts itself into a number through the valueOf method, and converts itself into a string through the toString method.
4. An object with a valueOf method should define a corresponding toString method to return string form of an equal number.
5. When detecting some undefined variables, typeOf should be used or compared with undefined, rather than using the truth value operation directly.
Interested friends can use itOnline HTML/CSS/JavaScript code running tool:http://tools./code/HtmlJsRunTest the above code running effect.
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript array operation skills》、《Summary of JavaScript sorting algorithm》、《JavaScript traversal algorithm and skills summary》、《Summary of JavaScript search algorithm skills"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.