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