Home  >  Article  >  Web Front-end  >  Two ways to determine odd and even numbers in JavaScript

Two ways to determine odd and even numbers in JavaScript

yulia
yuliaOriginal
2018-10-22 11:42:4645364browse

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

Two ways to determine odd and even numbers in JavaScript

Two ways to determine odd and even numbers in JavaScript

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 visit JavaScript 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn