Several points of attention in JS operation

Source: Internet
Author: User

1. In addition to the addition of string participation, when a value of a non-number type is evaluated, the values are converted to number and then evaluated

var res = true + +;
Console.log (RES); =101
var res1 = true + false;
Console.log (res1);//=1
var res2 = null + 10;
Console.log (res2);//=10
String subtraction will also be converted to number
var res1 = 100-' 123 ';
Console.log (res1);//=-23

2. Any value and Nan do operations are Nan

3. Any value and string do addition operations, will be converted to a string, and then the string to do the operation of the puzzle

4. Except 0 The result is infinite large infinity

<script>    var res = 100/0;    Console.log (res); // Infinity</script>

5. *= operator Operation Order

<script>    var a = +;     // A = A * (+ +)    Console.log (a); // =13000</script>

6. When a non-numeric value is compared with a value, it is converted to a number and then compared

    false // true    true // true    // true    NULL // true

7. If the values on either side of the symbol are strings, they are not converted to numbers for comparison, and the Unicode encoding of the characters in the string is compared separately

8. Null, undefined, nan comparison

Console.log (nullnull); // trueconsole.log (undefined = = undefined); // true // false

var num = NaN;
Console.log (IsNaN (num)); True
Console.log (null = = undefined); True
Console.log (Null = = = undefined); False

9. Logic Short Circuit

&&: The first one is false, the second will not continue to operate

|| : The first one is true and the second one will not continue the operation

10. Logical operation return value

//for non-Boolean numeric values, the logic participant automatically converts it to a Boolean type to determine//Logic and Operations//If condition A is established, no matter if condition B is not established, it returns the value of condition B itself.varresult = 123 && ' abc '; Console.log (result);//= ' abc 'varresult = 123 && 0; Console.log (result);//=0//if condition A is not true, return the value of condition a itselfvarresult =NULL&& 0; Console.log (result);//=null//Logical OR Operationvarresult = 123 | | ' ABC '; Console.log (result);//=123varresult =NULL|| 0; Console.log (result);//=0

11. A value is reversed two times, it does not change

How do you calculate and value the multiple comma-delimited expressions in the JS parentheses?

Left-to-right operation, followed by the value of the last expression

<script>var  num1, num2, num3, num4; var // result=8 var  = (a=3,--a, A * 5); Console.log (b); // b=10</script>

Several points of attention in JS operation

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.