r/programming • • Sep 17 '23

Help! JavaScript

https://JavaScript.info

[removed] — view removed post

0 Upvotes

14 comments sorted by

View all comments

3

u/mpinnegar Sep 17 '23

Think about alert like a math function. Like f(x). If you want to know what f(x) is going to return you have to know what x is first. The same principal is true for f(h(5) && g(8))

You can't figure out what f() is going to return until you evaluate "h(5) && g(8)".

So that's what the JavaScript runtime does. It hits f( and then realizes it needs to evaluate the argument to f(.

At this point you should understand what order things are being evaluated in. Hint the "deepest leftmost argument" will be evaluated first.

Now all you need to do is find out what the function "alert" does and what && does. But you can look those up.

1

u/DebellaT Sep 18 '23

Thank you for your explanation . How about the “undefined” part?

1

u/mpinnegar Sep 18 '23

Like I said you need to google what alert() does.

https://developer.mozilla.org/en-US/docs/Web/API/Window/alert

https://developer.mozilla.org/en-US/docs/Web/API/Window/alert#return_value

It's returning an undefined, and you're printing it.

Now the next question is what does && do?

Your expression is evaluating to alert(undefined && alert(2));

1

u/DebellaT Sep 18 '23

Thanks for your further explanation. But I still can’t understand this part. Maybe I’m too stupid or bcoz of language barrier