Yes, it is possible to do this without a flag variable. You can emulate for … else statements using a label and a block:

function search(num) {
    find: {
        for (var i of [0,1,2,3,4]) {
            if (i===num) {
               console.log("Match found: "+ i);
               break find;
            }
        } // else part after the loop:
        console.log("No match found!");
    }
    // after loop and else
}

That said, I would recommend against doing this. It is a very unconvential way of writing this and will lead to poor understanding or confusion. An early return is acceptable though, and can be used in a helper function if you need to continue with execution after the loop.

Answer from Bergi on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_if_else.asp
JavaScript else Statement
JS Examples JS HTML DOM JS HTML ... JS Interview Prep JS Bootcamp JS Certificate ... Use the else statement to specify a block of code to be executed if a condition is false....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › if...else
if...else - JavaScript - MDN Web Docs - Mozilla
May 4, 2026 - function checkValue(a, b) { if (a === 1) { if (b === 2) { console.log("a is 1 and b is 2"); } } else { console.log("a is not 1"); } } Do not confuse the primitive Boolean values true and false with truthiness or falsiness of the Boolean object. Any value that is not false, undefined, null, 0, -0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition. For example: js ·
🌐
freeCodeCamp
freecodecamp.org › news › javascript-if-else-and-if-then-js-conditional-statements
JavaScript If-Else and If-Then – JS Conditional Statements
August 9, 2021 - For example, if you are coding a bot, you can have it respond with different messages based on a set of commands it receives. In this article, I will explain what an if...else statement is and provide code examples.
🌐
W3Schools
w3schools.com › jsref › jsref_if.asp
JavaScript if/else Statement
Use else to specify a block of code to be executed, if the same condition is false
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Help please - for loop with if...else statement - JavaScript
February 19, 2021 - Hello, I’m trying to create code so when a user presses a button, X’s and O’s appear. The x’s and o’s should work in a pattern of x, xo, xox up to 16 x’s and o’s. I have created the button and I would like to use a for loop and an if, else statement.
🌐
Coderwall
coderwall.com › p › 02kr-g › javascript-thought-for-else-and-while-else-statements
JavaScript thought: for...else and while...else statements (Example)
February 25, 2016 - // Loop through an array of people: for (var i = 0, l = people.length; i < l; i++) console.log(people[i]); // If we didn't enter the previous loop: else console.log('No people!
Find elsewhere
🌐
Medium
geraldclarkaudio.medium.com › javascript-if-else-else-if-statements-26d18456f304
JavaScript — If, Else, & Else If Statements | by Gerald Clark | Medium
May 23, 2024 - Conditional statements are another way to control the flow of your code. The previous article went over looping through specific data and doing things based on that. If, else, and else if statements do the same thing except they don’t loop.
🌐
Programiz
programiz.com › javascript › if-else
JavaScript if...else Statement (with Examples)
How can we add multiple conditions within a single if...else statement? We can use logical operators such as && and || within an if statement to add multiple conditions. For example,
🌐
Romain Guillemot
rocambille.github.io › en › 2023 › 11 › 30 › for-else-in-javascript
For…else in JavaScript | Romain Guillemot
November 30, 2023 - <ul> {% for user in users %} <li>{{ user.username|e }}</li> {% else %} <li><em>no user found</em></li> {% endfor %} </ul>
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Conditionals
Making decisions in your code — conditionals - Learn web development | MDN
Conditional statements allow us to represent such decision making in JavaScript, from the choice that must be made (for example, "one cookie or two"), to the resulting outcome of those choices (perhaps the outcome of "ate one cookie" might be "still felt hungry", and the outcome of "ate two cookies" might be "felt full, but mom scolded me for eating all the cookies".) Let's look at by far the most common type of conditional statement you'll use in JavaScript — the humble if...else statement. Basic if...else syntax looks like this: js ·
🌐
GeeksforGeeks
geeksforgeeks.org › if-else-statement-in-javascript
if-else Statement in JavaScript - GeeksforGeeks
May 31, 2024 - In this article, we will learn about the conditional statement (ie, if-else statement) in Javascript, along with understanding its implementation through the examples. The conditional statement will perform some action for the specific condition.
🌐
Home and Learn
homeandlearn.co.uk › javascript › javascript_if_else.html
Javascript IF ... ELSE
If you add an else part, it needs its own pair of curly brackets. Miss one out and your code won't work. Between the curly brackets is where you type your code. Save your changes and run your code. You should see this written to your browser: ... So the first part of the IF Statement checks the condition between the round brackets. If it's true, the code for ...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_ifelse.htm
JavaScript - if...else Statement
Qualifies for driving Set the variable to different value and then try... The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-if-else
JavaScript if-else - GeeksforGeeks
July 28, 2025 - JS Tutorial · Web Tutorial · A to Z Guide · Projects · OOP · DOM · Set · Map · Math · Number · Boolean · Exercise · Last Updated : 28 Jul, 2025 · JavaScript conditional statements allow programs to make decisions based on specific conditions. They control the flow of execution, enabling different actions for different scenarios.
🌐
Mimo
mimo.org › glossary › javascript › else
JavaScript else statement: Learn to control program flow
Object-oriented programming often involves constructors, which can include if / else for initializing object properties: ... class User { constructor(name) { this.name = name || "Guest"; // Default name if falsy } } const user1 = new User("Alice"); const user2 = new User(); console.log(user1.name); // Alice console.log(user2.name); // Guest · When working with JSON, we often need if / else to handle missing or incorrect data:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-if-else-else-if
JavaScript if, else and else if | GeeksforGeeks
April 15, 2025 - The else statement follows an if statement and provides an alternative block of code to run when the condition in the if statement is false. It is used for creating conditional logic that handles both cases (true and false).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript if else
JavaScript if...else Statement
November 15, 2024 - This tutorial introduces you to JavaScript if...else statement that executes a block if a condition is true or another block otherwise.