This article is a detailed analysis of JS in the three-mesh operator and if else difference, is a very good article, recommended to everyone here.
Today wrote a picture of a small demo of the carousel, used to judge
try it first. If else, the code is as follows:
Copy CodeThe 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
Copy CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
var n=1;
if (n>1) {
n=0;
}else{
n++;
}
Console.log (n);
Output results: 2
The three mesh operation is as follows:
Copy CodeThe code is as follows:
var n=1;
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
Copy CodeThe code is as follows:
var n=1;
if (n>1) {
n=0;
}else{
++n;
}
Console.log (n);
Output results: 2
Results of trinocular operation
Copy CodeThe code is as follows:
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
Read this article, the small partners are not to JS in the three mesh operator and if else have a new understanding of it.
The difference analysis and example of the trinocular operator and if else in JS