Home > Web Front-end > JS Tutorial > How to determine odd and even numbers in javascript

How to determine odd and even numbers in javascript

WBOY
Release: 2021-12-07 14:35:17
Original
17209 people have browsed it

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;}".

How to determine odd and even numbers in javascript

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>
Copy after login

Output result:

How to determine odd and even numbers in javascript

How to determine odd and even numbers in javascript

##[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template