- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The else if statement in JavaScript is used to specify a new condition to test if the first condition is false. It allows you to chain multiple conditions together.
Example
let time = new Date().getHours();let greeting;if (time < 10) {greeting = "Good morning";} else if (time < 20) {greeting = "Good day";} else {greeting = "Good evening";}console.log(greeting); // Output will depend on the current timeCopied!✕CopyIn this example, the code checks the current hour and assigns a different greeting based on the time of day.
Important Considerations
Nesting Conditions
You can nest multiple else if statements to handle more complex conditions. However, be cautious with deeply nested conditions as they can make your code harder to read and maintain.
Using Blocks
Always use block statements {} to group multiple statements within an if, else if, or else clause. This helps avoid errors and makes your code more readable.
Truthy and Falsy Values
JavaScript else Statement - W3Schools
Use the else statement to specify a block of code to be executed if a condition is false. If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening": Use the else if statement to specify a …
See results only from w3schools.comW3Schools Exercise
Exercise: JS If Else True or False. JavaScript allows both lowercase if and …
W3Schools Tryit Editor
The W3Schools online code editor allows you to edit code and view the result in …
Random Link
The W3Schools online code editor allows you to edit code and view the result in …
Js Loop For In
The For In Loop The for...in loop iterates over the enumerable properties of an …
Js Switch
The break Keyword When JavaScript reaches a break keyword, it breaks out of …
Js Date Set Methods
Set Date methods let you set date values (years, months, days, hours, minutes, …
Function Definitions
Function Definitions vs Function Declarations Function definition is a …
Object Definitions
JavaScript Object fromEntries () ES2019 added the Object method fromEntries() …
Js Array Methods
JavaScript Array flatMap () ES2019 added the Array flatMap() method to JavaScript. …
Js RegExp
Regular Expressions A Regular Expression is a sequence of characters that forms a …
if...else - JavaScript | MDN
- 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.
JavaScript if, else and else if - GeeksforGeeks
Apr 15, 2025 · The else if statement allows you to check multiple conditions sequentially. It can be used when you want to test more than one condition and handle each case with a different block of code.
JavaScript if...else Statement (with Examples) - Programiz
- JavaScript if Statement. The syntax of the if statement is: if (condition) { // the …
- JavaScript if... else statement. An if statement can have an optional else …
- JavaScript if... else if statement. The if... else statement is used to execute a …
- Nested if... else Statement. You can also use an if... else statement inside of an …
- Body of if... else With Only One Statement. If the body of if... else has only one …
JavaScript if else else if - W3Schools
Use the else statement to specify a block of code to be executed if the condition is false. If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening": The result of greeting will be: …
JavaScript if else if
Learn how to use the if...else...if statement to check multiple conditions and execute the corresponding block if a condition is true. See examples of using the if...else...if statement to get the month name, …
JavaScript if-else, else-if Explained with Real Examples
Jun 23, 2025 · Learn JavaScript if, else, and else if conditional statements with syntax, use cases, and hands-on examples for smarter logic building. Every real …
Conditional branching: if, - The Modern JavaScript Tutorial
Learn how to use if, else, else if and conditional operator ? to perform different actions based on different conditions in JavaScript. See examples, syntax and …
if statement - "elseif" syntax in JavaScript - Stack Overflow
Oct 23, 2010 · How can I achieve an elseif in a JavaScript condition? In JavaScript's if-then-else there is technically no elseif branch. But it works if you write it this way: To make it obvious what is really …
JavaScript Conditionals: If, Else and Switch Explaination
Mar 14, 2026 · Learn JavaScript conditionals with simple examples. Understand if, else, else-if, and switch statements with clear explanations.
- People also ask