Is there any way to have a while-else loop in JavaScript?

twitter logo github logo ・1 min read

I am making a programming language in JavaScript,
as much of you lovely people know!
And I have encountered something....
I need to use a while-else loop....
but running:

while(conditionsMet) {

console.log("Conditions are met!")

} else {

console.log("Conditions are no longer met...")


will result in an error...
is there anything in JavaScript that is it's equivalence of python's:

while conditionsMet:
    print("Conditions are met!")
    print("Conditions are no longer met...")


twitter logo DISCUSS (6)
markdown guide

You can do

while(conditionsMet) {

console.log("Conditions are met!")


while(! conditionsMet ) {

console.log("Conditions are no longer met...")


This is not a correct behavior.
In Python while-else loop, the else block is executed at MOST once.


I've found a method I think works best, by merging all of the given ideas:

function while_else(conditions=false,body=function(){},exit=function(){}) {

if(conditions) {
if(!conditions) {

return 0


There are lot of other ways but it kind of depends on your use case usually javascript is written in more functional manner than python.


There are no exact equivalent syntax in JS.

Most of the time you can do:

while(conditionsMet) {
  console.log("Conditions are met!");
console.log("Conditions are no longer met...");

If you need the break / no-break behavior, you need to set a flag yourself, like...

let broken = false;
while(conditionsMet) {
  if (shouldBreak) {
    broken = true;
  console.log("Conditions are met!");
if (!broken) console.log("Conditions are no longer met...");
Classic DEV Post from May 10

Discuss: GitHub Special Event

GitHub is holding a "GitHub Special Event" today. Let's discuss what we're thinking leading up to it + while it's happening <3

PDS OWNER CALIN (Calin Baenen) profile image
I am a 13 (as of Oct 30 of 2019) yr/o developer who makes projects in languages like: Java, HTML, Python, JS, CSS, C, and am working on learning C++, and C#.

Sore eyes? now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️