The difference between the trinocular operator and if else in JS, do you understand?

Source: Internet
Author: User

Today wrote a picture of a small demo of the carousel, used to judge

Try it first. If else, the code is as follows: if (n >= count-1) {n = 0;} Else{n + +;} Then the code is finished, ready to optimize the code, the paragraph is changed to the three mesh operator n = n >= (count-1)? n=0:n++ the results are completely different. Then we study the difference between the two, summed up as a sentence: three mesh operation has a return value, if else no return value did the following test: Copy code var n=1; if (n>1) {n=0;} else{n++;} Console.log (n); output: 2 Copy the code three-mesh operation is as follows: var n=1;n = N>1?0:n++;console.log (n); the output is: 1 insert a section of other content: the difference between ++n and n++: Simply put, it is n self plus 1. The difference is that n++ is executed after the end of the statement only add 1, and ++n first do n+1 to execute the following statement then for ++n if Else statement copy code var n=1; if (n>1) {n=0;} else{++n;} Console.log (n); output: 2 Copy code results of three-mesh operation var n=1;n = n>1?0: ++n; Console.log (n); The output is: 2 You can see the difference between the if else and the three mesh. n++ and ++n in this verification, there is no difference, because if else is calculated after the result, does not return n, no return value but for the trinocular operation, the n value returned by the n++ is n itself, + + n Returns the result of the N value after n+1

The difference between the trinocular operator and if else in JS, do you understand?

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.