In this javascript example we will write how to a program to check a given number is even or odd. We can check number is even or odd in javascript by two ways
Using If-Else condition to check given number even or odd
<script> |
In the above example prompt the user to enter a number
Then converting it to integer
Now to check the number even we are divide the number with 2 and if the remainder '0' then number is Even otherwise 'odd'
Input:
33
output:
Given number is an odd number. |
Using Ternary Operator
Ternary JavaScript operator which will takes three operands
rather than the one or two that most operators use. It use shorten a simple if else block in JavaScript programs.
Let's check example
<script> |
Input:
22
Output:
This is an even number. |