Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You Don't Know JS: Types & Grammar - "What about &&" Code example not clear enough #948

Open
oriprice opened this issue Jan 17, 2017 · 3 comments · May be fixed by #1020
Open

You Don't Know JS: Types & Grammar - "What about &&" Code example not clear enough #948

oriprice opened this issue Jan 17, 2017 · 3 comments · May be fixed by #1020

Comments

@oriprice
Copy link

In What about &&? section we have the following code.

function foo() {
    console.log( a );
}

var a = 42;

a && foo(); // 42

result in comment should indicate that foo was called. but in fact we would get same result of "42" if first operand had been called (like in || operator). i think results of a and foo() should be different to make the example be clearer.

for example:

function foo() {
    console.log("foo");
}

var a = 42;

a && foo(); // "foo"
@ceckenrode
Copy link

ceckenrode commented Apr 29, 2017

We're checking to see if a exists to see if we should call a function which depends on a. So this makes sense if you already understand short circuit evaluation to a degree, but the example does feel a little contrived.

It might be clearer if we were doing something with a instead of doing more than just console logging a on its own? Example:

function foo() {
    console.log(a * 2);
}

var a = 42;

a && foo(); // "84"

@oriprice
Copy link
Author

yes, anything different than the value of a (42) is better.

@getify
Copy link
Owner

getify commented Aug 15, 2017

OK, fair point. We can tweak in second edition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants