Home > OS >  How to pass a bool value from called function back to the calling function
How to pass a bool value from called function back to the calling function

Time:08-17

Alright, so I was pretty sure I had the basics down, but maybe not, as I can't seem to understand why my bar() function won't receive the updated bool value from foo(). Would someone be able to help me understand where I'm going wrong here?

I'm sure it's probably something silly, but after digging around on the net and looking at some examples, I can't seem to get it quite right.

Expected Behaviour

bar() should output console.log('true');

Current Behaviour

bar() currently outputs console.log('false');

Thanks in advance.

class test {

  constructor() {
    this.bar();
  }

  foo(bool) {
    bool = true;
    return bool; // Return bool with 'true' value
  }

  bar() {
    let bool = false
    this.foo(bool);
    console.log(bool); // Console the updated value.
  }

}
new test();

CodePudding user response:

You're not setting the return value. Change your bar function:

bar() {
    let bool = false
    bool = this.foo(bool);
    console.log(bool); // Console the updated value.
}
  • Related