Using a function as statement in while loop JavaScript?

Tags: , , ,

I believe similar questions have been posted before, but I don’t entirely understand why using a callback function as a While loop statement, such as seen below, results in an infinite loop:

do {
} while(myFunc);

function myFunc(){
    return false;

This version, on the other hand, prints “repeat” once and then stops:

do {
} while(myFunc === false);

Why is that?


myFunc is a variable (all functions in JS are object variables), and it’s not equal to false.

myFunc() on the other hand, is the results of myFunct being called, and is equal to false.

So you should compare the result of the function, not the function itself, by calling it:

do {
} while(myFunc() === false);

Source: stackoverflow