![]() |
The if…else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed. This structure facilitates decision-making in JavaScript programs. We have the following conditional statements available in JavaScript: Table of Content if statementThe if statement is defined using the if keyword and is always operated with an expression passed inside the round brackets just after its definition. Syntax: if (conditional_expression){ Example: The below code implements the if statement practically with an expression in JavaScript.
Output 45 is less than 50 5 completely divides 45 if else statementThe if else statement is an extended version of the if condition. It allows you to write code for both the cases i.e. either the expression returns true or false, you can handle both of them. The if statement will run for the true value while else for the false value. Syntax: if (conditional_expression){ Example: The below code will explain the use of the if else statement in JavaScript.
Output 45 is less than 50 45 is not divisible by 8 else if statementThe else if statement is used in the cases where you need to execute multiple code blocks based on multiple conditional expression. It starts with the if statement and ends with the else statement, it is written in between them. Syntax: if (conditional_expression1){ Example: The below code will explain the practical use of the else if statement in JavaScript.
Output 45 is less than 50 The sum of numbers 45 and 8 is greater than 50. The difference of numbers 45 and 8 is less than 50. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |