Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, February 12, 2023

if..else if..else statement in JavaScript



if..else if..else statement in JavaScript




The if..else if..else statement in JavaScript is a conditional statement that allows you to test multiple conditions and execute different blocks of code based on those conditions.


if..else if..else statement in JavaScript


Syntax:
The basic syntax of the if...else if...else statement is as follows:



In this syntax, condition1, condition2, condition3, etc. are expressions that can be either true or false. The if statement tests each condition in order, and the first condition that is true determines which block of code will be executed. If none of the conditions are true, the code inside the else block will be executed.


Example



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 if..else statement in JavaScript then tests the value of the number variable and displays a message based on its value.

The first condition number > 0 tests if the number is greater than 0. If this condition is true, the code inside the first if block will be executed, and the message "number is a positive number." will be displayed in the console.

If the first condition is false, the second condition number < 0 tests if the number is less than 0. If this condition is true, the code inside the second else if block will be executed, and the message "number is a negative number." will be displayed in the console.

If both conditions are false, the code inside the else block will be executed, and the message "number is zero." will be displayed in the console.

This program demonstrates how to use the if..else if..else statement in JavaScript to make a simple decision based on multiple conditions and execute different blocks of code depending on the value of the input number.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages