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?