What is the difference between the trinocular operator and the If else in JavaScript

Source: Internet
Author: User

What is the difference between the trinocular operator and the If else in JavaScript
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 changed to the three-mesh operator of the wording
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
The following tests were done:
var n=1; if (n>1) {
n=0;
}else{
n++;
}
Console.log (n);
Output results: 2
The three-mesh operation is as follows:
var n=;
n = n>1?0:n++;
Console.log (n);
Output Result: 1
Insert a section of other content: the difference between ++n and n++: Simply put, it's n-plus 1. The difference is that the n++ is executed after the end of the statement only add 1, and ++n first do n+1 to execute the following statement

So what about ++n?
If Else statement
var n=1; if (n>1) {
n=0;
}else{++n;
}
Console.log (n);
Output results: 2
Results of trinocular operation
var n=1;
n = n>1?0: ++n;
Console.log (n); Output Result: 2

You can see the difference between the if else and the three-mesh operation.
n++ and ++n in this validation, there is no difference, because if else is the result of the calculation, does not return n, no return value
But for the trinocular operation, the n value returned by n++ is n itself, and the ++n returns the N value as the result after n+1

Source: http://www.codes51.com/article/detail_94371.html

What is the difference between the trinocular operator and the If else in JavaScript

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.