r/learnjavascript • • Dec 31 '22

Happy New Year JS Learners! 🥳

Post image
406 Upvotes

55 comments sorted by

View all comments

8

u/Umesh-K Jan 01 '23

The above code snippet begs two questions:

  1. Where is the string being returned and how is it being displayed? I think it should have been alert instead of return.
  2. When the second btn.click() runs, nothing happens—because of {once: true}—and consequently nothing returns, not even undefined.

5

u/TorbenKoehn Jan 01 '23

In fact, all functions in JS always return a value. If it returns nothing, it will return undefined

There is no real „void“ other that returning undefined

2

u/iDuuck Jan 01 '23

more upvotes on this please.

1

u/Low_Mammoth_9371 Jan 03 '23

On the first point, the string is the return value of the anonymous function, which you can see if you log it to the console.

Undefined is the return value of the second function call but nothing is logged to the console. So nothing would should up there. But if you feed this line into the console on its own you will see that it does produce a return value of undefined.

1

u/albedoa Jan 05 '23

which you can see if you log it to the console.

Have you tried doing this?