The JavaScript data type is very weak (otherwise it will not be called a weak type language )! When using arithmetic operators, the Data Types on both sides of the operators can be arbitrary, for example, A string can be added with numbers. The JavaScript data type is very weak (otherwise it will not be called a weak type language )! When Arithmetic Operators are used, the Data Types on both sides of the operators 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 that the JavaScript engine quietly converted them by implicit type before the operation. The following is the addition of the value type and boolean type:
The Code is as follows:
3 + true; // 4
The result is a numeric value! If it is in the C or Java environment, the above operation will certainly result in an error because of Inconsistent Data Types on both sides of the Operator! However, in JavaScript, the error type can only cause errors in a few cases, such as calling a non-function or reading a null or undefined attribute, as follows:
The Code is as follows:
"Hello" (1); // error: not a function
Null. x; // error: cannot read property 'X' of null
In most cases, JavaScript will not go wrong, but will automatically convert the corresponding type. For example, arithmetic operators such as-, *,/, and % convert the operands into numbers, but the "+" sign is a bit different. In some cases, it is an arithmetic plus sign, in some cases, it is a string Concatenation symbol, which depends on its operations, as follows:
The Code is as follows:
2 + 3; // 5
"Hello" + "world"; // "hello world"
However, what will happen if the strings and numbers are added together? JavaScript will automatically convert the number into a character, whether the number is in front or the string is in front, as follows:
The Code is as follows:
"2" + 3; // "23"
2 + "3"; // "23"
The result of adding a string and a number is a string. The important thing is to say three times !!!!!!
In addition, note that the Operation direction of "+" is from left to right, as shown below:
The Code is as follows:
1 + 2 + "3"; // "33"
This is equivalent to the following:
The Code is as follows:
(1 + 2) + "3"; // "33"
In contrast, the following results are different:
The Code is as follows:
1 + "2" + 3; // "123"
However, implicit type conversion sometimes hides some errors. For example, null is converted to 0, and undefined is converted to NaN. It should be noted that NaN and NaN are not equal (this is determined by the precision of floating point numbers), as follows:
The Code is as follows:
Var x = NaN;
X = NaN; // false
Although JavaScript provides isNaN to check whether a value is NaN, it is not very accurate because there is an implicit conversion process before the isNaN function is called, it will convert those values that are not NaN to NaN, as follows:
The Code is as follows:
IsNaN ("foo"); // true
IsNaN (undefined); // true
IsNaN ({}); // true
IsNaN ({valueOf: "foo"}); // true
The above code, we use isNaN for testing, and found that the string, undefined, or even object, returns true results !!! But we can't say they are NaN, right? All in all, the conclusion is: isNaN detection NaN is not reliable !!!
Fortunately, there is a reliable and accurate method for detecting NaN. We all know that only NaN does not support itself, so we use the non-equal sign (! =) To determine whether a number is equal to itself, so that NaN can be detected, as shown below:
var a = NaN;a !== a; // truevar b = "foo";b !== b; // falsevar c = undefined;c !== c; // falsevar d = {};d !== d; // falsevar e = { valueOf: "foo" };e !== e; // false
We can also define this mode as a function, as shown below:
function isReallyNaN(x) {return x !== x;}
OK, NaN's detection method is so simple. We will continue to discuss implicit conversion of objects below!
The object can be converted to the original starting value. The most common method is to convert it into a string, as shown below:
"the Math object: " + Math; // "the Math object: [object Math]""the JSON object: " + JSON; // "the JSON object: [object JSON]"
The toSting function is called when the object is converted to a string. You can manually call it to check whether:
Math.toString(); // "[object Math]"JSON.toString(); // "[object JSON]"
Similarly, an object can be converted to a number through the valueOf function. Of course, you can also customize this valueOf function, as shown below:
"J" + { toString: function() { return "S"; } }; // "JS"2 * { valueOf: function() { return 3; } }; // 6
If an object has both the valueOf method and toString method, the valueOf method will always be called first, as shown below:
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. Generally, try to make valueOf the same value as toString (although the type can be different ).
The last forced type conversion is often called "True Value Calculation", for example, if, |, &. Their operands are not necessarily boolean values. JavaScript converts non-boolean values to boolean values through simple conversion rules. Most values are converted to true. Only a few values are false. They are: false, 0,-0, "", NaN, null, undefined, because the values of numbers, strings, and objects are false, it is not safe to directly use True Value conversion to determine whether a function parameter is passed in. For example, there is a function that can have default optional parameters, as shown below:
function point(x, y) {if (!x) {x = 320;}if (!y) {y = 240;}return { x: x, y: y };}
This function ignores any parameter whose true value is false, including 0 and-0;
The Code is as follows:
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 method can be used to separate 0 and undefined:
point(); // { x: 320, y: 240 }point(0, 0); // { x: 0, y: 0 }
Another method is to compare the parameter with undefined, as shown below:
if (x === undefined) { ... }
Summary:
1. Type errors may be hidden by type conversion.
2. "+" can represent both string join and arithmetic addition, depending on its operands. If one is a string, it is a string connection.
3. Use the valueOf method to convert an object into a number and use the toString method to convert itself into a string.
4. For objects with the valueOf method, a corresponding toString method should be defined to return strings of equal numbers.
5. When detecting some undefined variables, you should use typeOf or compare it with undefined, instead of directly using True-value operations.
Here we will introduce the implicit type conversion in JavaScript, and hope to help you!