if..else Statement in JavaScript
Output:
The above program uses the prompt function to ask the user to enter a number. The input number is stored in the number variable.
The if...else statement then tests whether the number is even or odd. To check if a number is even, we can use the modulo operator % to find the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it is odd.
In the if statement, the condition number % 2 === 0 checks if the remainder of number divided by 2 is equal to 0. If this condition is true, the code inside the if block will be executed, and the message "number is an even number." will be displayed in the console.
If the condition is false, the code inside the else block will be executed, and the message "number is an odd number." will be displayed in the console.
This program demonstrates how to use the if..else statement in JavaScript to make a simple decision based on a condition and execute different blocks of code depending on whether the condition is true or false.
No comments:
Post a Comment