Operator type conversion in JavaScript sample Summary _ Basics

Source: Internet
Author: User
Tags numeric value

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!

Implicit conversions of operator strings
Multiplication

 Console.dir ("-------The multiplication---------below");
 Console.dir (5* "5");
 Console.dir (5* "a");
 Console.dir (5*nan);
 Console.dir (5*null);
 Console.dir (5*undefined);
 Console.dir (5*5);
 Console.dir ("Multiplication---------above-------");

Division

 Console.dir ("-------the following division---------");
 Console.dir (5/"5");
 Console.dir (5/"a");
 Console.dir (5/nan);
 Console.dir (5/null);
 Console.dir (NULL/5);
 Console.dir (5/undefined);
 Console.dir (5/5);
 Console.dir (5/0);
 Console.dir (0/5);
 Console.dir (0/0);
 Console.dir ("-------Division---------"); 

Take the remainder, the model

 Console.dir ("-------the following, modulus--------");
 Console.dir (16% "5");
 Console.dir (5% "a");
 Console.dir (5%nan);
 Console.dir (5%null);
 Console.dir (null%5);
 Console.dir (5%undefined);
 Console.dir (5%5);
 Console.dir (5%0);
 Console.dir (0%5);
 Console.dir (0%0);
 Console.dir ("-------above, for mold---------"); 

Addition

 Console.dir ("-------The following addition--------");
 Console.dir (16+ "5");
 Console.dir (5+ "a");
 Console.dir (5+nan);
 Console.dir (5+null);
 Console.dir (5+undefined);
 Console.dir (5+5);
 Console.dir ("Two number of the and is" +5+5);
 Console.dir ("Two number of and is" + (5+5));
 Console.dir ("-------addition--------"); 

Subtraction

 Console.dir ("-------subtraction--------");
 Console.dir (16-"5");
 Console.dir ("a");
 Console.dir (5-nan);
 Console.dir (5-null);
 Console.dir (5-undefined);
 Console.dir (5-5);
 Console.dir (5-true);
 Console.dir ("true");
 Console.dir ("");
 Console.dir ("The difference of two numbers is" +5-5);
 Console.dir ("Two number of difference is" + (5-5));
 Console.dir ("-------subtraction--------"); 

Relational operators

 Console.dir ("-------The following relational operator--------");
 Console.dir (16> "5");
 Console.dir ("A" > "5");
 Console.dir (5< "a");
 Console.dir (5>=nan);
 Console.dir (5<nan);
 Console.dir (Nan>=nan);
 Console.dir (5>=null);
 Console.dir (5>=undefined);
 Console.dir (5>=5);
 Console.dir (5>=true);
 Console.dir (5>= "true");
 Console.dir (5>= "");
 Console.dir ("Brick" > "alphabet");
 Console.dir ("Brick" > "alphabet");
 Console.dir ("-------above relational operator--------"); 

Multiplication

 Console.dir (5* "5");
 Console.dir (5* "a");//nan
 Console.dir (5*nan);//nan
 Console.dir (5*null); 0
 Console.dir (5* undefined);//nan
 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

 Console.dir (5/"5");//1 converts  characters to numbers for division
 console.dir (5/"a"),//nan converts  "a" with the number () function, the value is Nan, and the result is Nan
 Console.dir (5/nan);//nan
 Console.dir (5/null);//infinity Null is converted with the number () function, the result is 0, then 5/0 is positive infinity
 Console.dir (NULL/5);//0 Ibid. 0/5 is 0
 console.dir (5/undefined);//nan  undefined is converted with number () and the result is NaN
 Console.dir (5/5),//1
 Console.dir (5/0),//infinity console.dir
 (0/5);//0 console.dir
 (0/0);//nan// 0 divided by 0, and 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:

 Console.dir (16% "5"); 1 converts the string 5 through number () to 5 and then the remainder
 console.dir (5% "a");//nan
 Console.dir (5%nan)//nan
 console.dir (5%null) //nan NULL is converted by number (), the result is 0, then the 5%0 is calculated, and the result is NaN
 console.dir (null%5);//0 ditto 0%5, the result is 0
 console.dir (5% undefined);//nan console.dir (5%5);//0 Console.dir (5%0);//nan console.dir (
 0%5);//0 Console.dir (
 0%0);//nan
Console.dir (infinity%infinity);//nan console.dir (
5%infinity);//5 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!

 Console.dir (16-"5");//11 Console.dir ("a");//nan Console.dir (5-nan);//nan console.dir
 (5-null);//5
 Console.dir (5-undefined),//nan
 Console.dir (5-5),//0 console.dir
 (5-true),//4 console.dir
 (5-" True ");//nan
 Console.dir (" ");//5
 Console.dir (5-infinity);//-infinity
 Console.dir ( infinity-infinity);//nan
 Console.dir ("The difference of two numbers is" +5-5)//nan
 console.dir ("The difference of two numbers 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

Relational operator unification returns TRUE or False

 Console.dir (16> "5"); True
 Console.dir ("> 5");//false
 Console.dir (5< "a");//false
 Console.dir (5>=nan);// False
 Console.dir (5<nan);//false
 Console.dir (Nan>=nan);//false
 Console.dir (5>=null);// True
 Console.dir (5>=undefined);//false
 Console.dir (5>=5);//true
 Console.dir (5>=true);// True
 Console.dir (5>= "true");//false
 Console.dir (5>= "");//true 
 console.dir ("Brick" > Alphabet "); the string encoding value of//false B is 66, and the string encoding of a is 97. So false
 Console.dir (" Brick ">" alphabet ");//true Small Letter B is larger than a, So it's 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.

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!

 Console.dir (16+ "5"); 156
 Console.dir (5+ "a");//5a Console.dir (5+nan);//nan console.dir (
 5+null);//5 console.dir
 (' 5 ' +null);//5null
 Console.dir (5+undefined);//nan Console.dir (null+undefined);//nan console.dir
 (5+5); /10
 Console.dir ("Two number and is" +5+5);//two number and is
 console.dir ("Two number and is" + (5+5));//two number and is 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.

Implicit conversion of double equals sign
run the following code again, I believe you naturally understand the ~

var A;

Console.dir (0 = false);//true

console.dir (1 = = true);//true

Console.dir (2 = = {Valueof:function () {return 2}}) //true

console.dir (a = = Nan);//false
console.dir (nan = = nan);//false

console.dir (8 = undefined); False

console.dir (1 = undefined);//false

Console.dir (2 = = {Tostring:function () {return 2}});//true

Console.dir (undefined = = null);//true

console.dir (null = = 1);//false

Console.dir ({tostring:function () { Return 1}, Valueof:function () {return []} = = = 1);//true

Console.dir (1== "1");//true

Console.dir (1=== "1"); False

Related Article

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.