Home > Article > Web Front-end > How to determine odd and even numbers in javascript
In JavaScript, you can use the if statement, "%" and "==" operators to determine whether the value is divisible by 2, and then determine whether the value is an odd or even number. The syntax is "if(value%2= =0){Even number execution code;}else{Odd number execution code;}".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
How to find odd and even numbers in JavaScript
In JavaScript, you can use if statements, % and == operators to find odd and even numbers, example As follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <body> <input type="text" id="input1" value="" /> <button id="btn">点击</button> </body> <script type="text/javascript"> btn.onclick=function(){ if(input1.value%2==0){ alert("这是个偶数"); }else{ alert("这是个奇数"); } </script> </html>
Output result:
##[Recommended learning:javascript advanced tutorial]
The above is the detailed content of How to determine odd and even numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!