Home>Article>Web Front-end> Two ways to determine odd and even numbers in JavaScript
JavaScript is an essential part of front-end development, so do you know how to use JS to determine whether a number is odd or even? This article will share with you two methods of judging odd and even numbers in JS. It has certain reference value. Interested friends can refer to it.
Method 1: Use the if function to determine whether the number is divisible by 2
Description: Use the if function to determine whether the number is divisible by 2. If it is divisible, an even number will pop up. If If not, it is an odd number. The specific code is as follows:
The effect is as shown in the picture. Obviously 33 is an odd number
Method 2: Use the ternary operator to determine whether the number is divisible by 2
Description: Get the input value, and use the ternary operator to determine whether the number is divisible by 2. If so, Then an even number will pop up. If not, it will be an odd number. The specific code is as follows:
The above introduces you to the two methods of JS to determine whether a number is odd or even. The principle is the same, but the implementation method is different. Beginners You can try it yourself, I hope this article will be helpful to you!
For more related video tutorials, please visitJavaScript Tutorial
The above is the detailed content of Two ways to determine odd and even numbers in JavaScript. For more information, please follow other related articles on the PHP Chinese website!