Lewis
Lewis

Reputation: 2188

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

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();

Upvotes: 0

Views: 28

Answers (1)

Yinci
Yinci

Reputation: 2480

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.
}

Upvotes: 1

Related Questions