Home >Web Front-end >JS Tutorial >Analysis and examples of the difference between ternary operator and if else in JS_javascript skills
Today I wrote a small demo of image carousel, using judgment
I tried if else first, the code is as follows:
After the code is written, I am going to optimize the code and change this section to the ternary operator
The results are completely different
Then I studied the difference between the two and summarized it in one sentence: ternary operation has a return value, if else has no return value
The following tests were done:
Output result: 2
Ternary operation is as follows:
Insert a piece of other content: The difference between n and n: Simply put, n increases by 1. The difference is that n is incremented by 1 after executing the following statement; while n is done by n 1 before executing the following statement
What about n
if else statement
Ternocular operation results
After reading this article, do you guys have a new understanding of the ternary operator and if else in js?