0

Let's say I have a function like this:

checkerFunction() {

  let checker = false;
  // Note here I only do a return if the *if* condition is fulfilled
  if (condition) {
    checker = true;
    return checker;
  }
}

And then I use it as a true/false check on another function:

anotherFunction() {
 // What will happen here if there's no return from this.checkerFunction()
 if (this.checkerFunction()) {
   somethingHappens;
 }
}

What will happen in

if (this.checkerFunction())

if there's no return in checkerFunction() because the condition was never true... Is it going to be interpreted as true/false or is this going to be an error and why?

Javier Ortega
  • 147
  • 10
  • 1
    function with no return will return `undefined`... that's actually the value having a variable not being initialized .. so semantically speaking it doesn't return anything – Diego D Jun 08 '22 at 10:08
  • It will just return undefined which is a falsy value. Testing this should be easy enough in the browser's console, too. – Stratis Dermanoutsos Jun 08 '22 at 10:12

1 Answers1

7

Functions which end without hitting a return statement will return undefined (or a promise that resolves to undefined if they are async).

undefined is a falsy value.

This is trivial to test:

function foo () {
    // Do nothing
};

const result = foo();

if (result) {
    console.log("A true value: ", result);
} else {
    console.log("A false value: ", result);
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335