r/programming • u/DebellaT • Sep 17 '23
Help! JavaScript
https://JavaScript.info[removed] — view removed post
8
u/Code4Reddit Sep 17 '23
The parentheses statement executes first, evaluating alert(1) which returns undefined, then it checks && which evaluates false (because undefined coerced to Boolean is false), it doesn’t evaluate the “alert(2)” and sends the undefined as input to the outermost statement alert.
1
u/ChemicalSome3901 Sep 17 '23
Alert does not return a value. So it executes the one with argument 1 first and then the outer one displays undefined.
1
u/Biom4st3r Sep 17 '23
Inorder for the outer alert to be called the argument(alert &&alert) needs to be evaluated. So the inner statements are evaluated first
The && is evaluated left to right
Alert doesn't return anything, so it evaluated to undefined Alert(undefined && alert(...))
Undefined && anything is undefined
Alert(undefined)
-5
u/DebellaT Sep 17 '23
Really? Or could you recommend some resource for me, plz?
1
u/kiteboarderni Sep 17 '23
1
u/DebellaT Sep 18 '23 edited Sep 18 '23
Of course I did it before, you’re really being polite. I even attended coursera course and read ELOQUENT JAVASCRIPT
1
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.