JS operator type conversion encyclopedia (front side of the question operator)

Source: Internet
Author: User
Tags object numeric numeric value string

operator operators, in the front-end interview topics often appear, many friends in their operations often error. Description of the operator operation is not fully understood, I earlier in an article, JavaScript double equals type conversion, mainly on the double equals caused by the type conversion. This article can be said to be a supplement to this article!

First, let's do some questions first! In order to unify, I do not mix to write these questions, the interview topic, often will these questions mix up, such to your confusion will be bigger, in order to be more convenient demonstration, I here the module has written some questions, everybody may look down!
 
 
  1. Implicit conversions of operator strings
  2. Multiplication
  3. Console.dir ("-------The multiplication---------below");
  4. Console.dir (5* "5");
  5. Console.dir (5* "a");
  6. Console.dir (5*nan);
  7. Console.dir (5*null);
  8. Console.dir (5*undefined);
  9. Console.dir (5*5);
  10. Console.dir ("Multiplication---------above-------");
  11. Division
  12. Console.dir ("-------the following division---------");
  13. Console.dir (5/"5");
  14. Console.dir (5/"a");
  15. Console.dir (5/nan);
  16. Console.dir (5/null);
  17. Console.dir (NULL/5);
  18. Console.dir (5/undefined);
  19. Console.dir (5/5);
  20. Console.dir (5/0);
  21. Console.dir (0/5);
  22. Console.dir (0/0);
  23. Console.dir ("-------Division---------");
  24. Take the remainder, the model
  25. Console.dir ("-------the following, modulus--------");
  26. Console.dir (16% "5");
  27. Console.dir (5% "a");
  28. Console.dir (5%nan);
  29. Console.dir (5%null);
  30. Console.dir (null%5);
  31. Console.dir (5%undefined);
  32. Console.dir (5%5);
  33. Console.dir (5%0);
  34. Console.dir (0%5);
  35. Console.dir (0%0);
  36. Console.dir ("-------above, for mold---------");
  37. Addition
  38. Console.dir ("-------The following addition--------");
  39. Console.dir (16+ "5");
  40. Console.dir (5+ "a");
  41. Console.dir (5+nan);
  42. Console.dir (5+null);
  43. Console.dir (5+undefined);
  44. Console.dir (5+5);
  45. Console.dir ("Two number of the and is" +5+5);
  46. Console.dir ("Two number of and is" + (5+5));
  47. Console.dir ("-------addition--------");
  48. Subtraction
  49. Console.dir ("-------subtraction--------");
  50. Console.dir (16-"5");
  51. Console.dir ("a");
  52. Console.dir (5-nan);
  53. Console.dir (5-null);
  54. Console.dir (5-undefined);
  55. Console.dir (5-5);
  56. Console.dir (5-true);
  57. Console.dir ("true");
  58. Console.dir (5-"");
  59. Console.dir ("The difference of two numbers is" +5-5);
  60. Console.dir ("Two number of difference is" + (5-5));
  61. Console.dir ("-------subtraction--------");
  62. Relational operators
  63. Console.dir ("-------The following relational operator--------");
  64. Console.dir (16> "5");
  65. Console.dir ("A" > "5");
  66. Console.dir (5< "a");
  67. Console.dir (5>=nan);
  68. Console.dir (5
  69. Console.dir (Nan>=nan);
  70. Console.dir (5>=null);
  71. Console.dir (5>=undefined);
  72. Console.dir (5>=5);
  73. Console.dir (5>=true);
  74. Console.dir (5>= "true");
  75. Console.dir (5>= "");
  76. Console.dir ("Brick" > "alphabet");
  77. Console.dir ("Brick" > "alphabet");
  78. Console.dir ("-------above relational operator--------");

Then your conversion to the operator type is basically understandable. If you have a mistake, then let's explain it in detail!

Multiplication

Let's take a look at the previous topic:

 
  
 
  1. Console.dir (5* "5"); 25
  2. Console.dir (5* "a");//nan
  3. Console.dir (5*nan);//nan
  4. Console.dir (5*null); 0
  5. Console.dir (5*undefined);//nan
  6. Console.dir (5*5);//25

Here are the multiplication implicit conversion principles:

1, if 2 numeric values are numbers, then direct multiplication, (I believe everyone will, and primary mathematics, as well as to pay attention to the number of symbols), if multiply charge number value than the ECMAScript range of numerical representation, Returns either Infinity (positive infinity) or-infinity (negative infinity)

2, if a number is Nan, then the result is Nan

3, if the infinity is multiplied by 0, the result is Nan

4, if an operator is a number, the other is not a numerical value, then the number () function, it is converted, the converted values and numbers to multiply. If the converted result appears Nan, then the result is Nan.

Division

Let's take a look at the above question!

 
  
 
  1. Console.dir (5/"5");//1 converts characters to numbers to divide
  2. Console.dir (5/"a");//nan converts "a" with the number () function, the value is Nan, and the result is Nan
  3. Console.dir (5/nan);//nan
  4. Console.dir (5/null);//infinity Null is converted with the number () function, the result is 0, then 5/0 is positive infinity
  5. Console.dir (NULL/5)//0 Ibid. 0/5 is 0
  6. Console.dir (5/undefined);//nan undefined is converted with number () and the result is NaN
  7. Console.dir (5/5);//1
  8. Console.dir (5/0);//infinity
  9. Console.dir (0/5);//0
  10. Console.dir (0/0);//nan//0 divided by 0 The result is NaN

Here is the principle of division recessive conversion:

Like multiplication, the only one is 0/0, and the result is Nan.

Take the remainder, the model

The most used in the project is the odd even number. We often use a number and 2 for the remainder, the result is 0 then this number is even, the result is 1 then this number is odd.

Look at the above topic:

 
  
 
  1. Console.dir (16% "5"); 1 converts the string 5 through number () to 5 and then the remainder
  2. Console.dir (5% "a");//nan
  3. Console.dir (5%nan);//nan
  4. Console.dir (5%null);//nan converts null through number (), the result is 0, and then the 5%0 is calculated, and the result is NaN
  5. Console.dir (null%5)//0 Ibid 0%5, the result is 0
  6. Console.dir (5%undefined);//nan
  7. Console.dir (5%5);//0
  8. Console.dir (5%0);//nan
  9. Console.dir (0%5);//0
  10. Console.dir (0%0);//nan
  11. Console.dir (infinity%infinity);//nan
  12. Console.dir (5%infinity);//5
  13. Console.dir (infinity%5); NaN

Here's a look at the principle of implicit conversion:

As with multiplication, let me say something more special. We all know the concept of divisor and divisor, we learned it in primary school.

1, dividend is infinite, the divisor is a finite value, then the result is Nan

2, dividend is a finite value, the divisor is 0, then the result is Nan

3, infinity%infinity result is Nan

4, dividend is a finite value, divisor is the value of infinity, the result is a divisor.

5, Dividend is 0, the result is 0

Subtraction

Take a look at the example above!

 
  
 
  1. Console.dir (16-"5");//11
  2. Console.dir ("a");//nan
  3. Console.dir (5-nan);//nan
  4. Console.dir (5-null);//5
  5. Console.dir (5-undefined);//nan
  6. Console.dir (5-5);//0
  7. Console.dir (5-true);//4
  8. Console.dir ("true");//nan
  9. Console.dir (5-"");//5
  10. Console.dir (5-infinity);//-infinity
  11. Console.dir (infinity-infinity);//nan
  12. Console.dir ("The difference of two numbers is" +5-5);//nan
  13. Console.dir ("Two number of difference is" + (5-5));//two difference is 0

Here's how to subtract the implicit conversion principle:

Like the above, the same will not be said, I said the specific subtraction.

1, infinity-infinity result is Nan

2,-infinity-infinity result is-infinity

3, a digital subtraction Infinity result is-infinity

4, infinity-(-infinity) result is Infinity

5, if the operand is an object, call the object valueof method, if the result is Nan, then the result is Nan. If there is no valueof method, the ToString () method is invoked and the resulting string is converted to a numeric value.

Relational operators

Let's take a look at the example above, the relational operator unifies returns TRUE or False

 
  
 
  1. console.dir (16> "5");  //true 
  2.  console.dir (" "> 5");//false 
  3.  console.dir (5< "a");//false 
  4.   Console.dir (5>=nan);//false 
  5.  console.dir (5
  6.  console.dir (Nan>=nan);//false 
  7.  console.dir (5>=null);//true 
  8.  console.dir (5>=undefined);//false 
  9.  console.dir (5>=5); true 
  10.  console.dir (5>=true);//true 
  11.  console.dir (5>= " True ");//false 
  12.  console.dir (5>=" ");//true  
  13.   Console.dir ("Brick" > "alphabet");//false  b string encoding value is 66 , and A's string encoding is 97. therefore false 
  14.  console.dir ("Brick" > "alphabet");//true  lowercase b is larger than a, so it is true 

Let's say the implicit conversion principle of relational operators:

Or the same as above, the same will not say.

If the two numbers that are compared are strings, the string encoding values corresponding to the strings are compared.

Implicit conversion of double equals sign

Double equals details please see: http://www.haorooms.com/post/js_yinxingleixing

Addition operations

The implicit conversion of the additive operation, I finally said, is because the addition of the implicit conversion and before the same, all the previous operation symbols, as long as one is a number, and the other by default using number () for digital conversion. The addition operation is not the same. The addition operation as long as one of the strings, then the other will also be converted to a string, and then the concatenation of strings!

Take a look at the first example!

 
  
 
  1. Console.dir (16+ "5"); 156
  2. Console.dir (5+ "a");//5a
  3. Console.dir (5+nan);//nan
  4. Console.dir (5+null);//5
  5. Console.dir (' 5 ' +null);//5null
  6. Console.dir (5+undefined);//nan
  7. Console.dir (null+undefined);//nan
  8. Console.dir (5+5);//10
  9. Console.dir ("Two number of and is" +5+5);//two number and 55
  10. Console.dir ("Two number and is" + (5+5));//two number and 10

Here's a look at the implicit conversion principle of the addition operator:

1, there is a string, then the other one will also be converted to string concatenation. If one is a string and the other is null or undefined, then the addition, NULL, or undefined will call the string () method, get the string "null" or "undefined", and then splice.

2. If a number is added null or undefined, then the null or undefined is converted to numbers () and then added.

3, the rest of the principles and other similar, do not say more.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.