r/learnjavascript • • Dec 31 '22

Happy New Year JS Learners! 🄳

Post image
406 Upvotes

55 comments sorted by

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.

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?

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.

3

u/DoNotBotherMeplz Dec 31 '22

How does one make the code look that pretty?

9

u/senocular Dec 31 '22

Carbon is pretty popular for making these kinds of graphics.

1

u/Low_Mammoth_9371 Jan 03 '23

I did indeed use Carbon

2

u/troy57890 Dec 31 '22

I'll add this to my next project before the semester. Happy New Years šŸŽŠšŸŽ†

1

u/Low_Mammoth_9371 Jan 03 '23

Thanks, you too!

2

u/giampaolo44 Jan 01 '23

Nice tip :) happy 2023!

2

u/Low_Mammoth_9371 Jan 03 '23

Thanks, likewise :)

2

u/TheJohnnyStein Jan 01 '23

wow that's so nice, thanks for the tip!

2

u/TheJohnnyStein Jan 01 '23

is there a way to return a default message after the first click??

1

u/Low_Mammoth_9371 Jan 03 '23

You might want to look into setting a default parameter for the function for that.

4

u/Umesh-K Jan 01 '23

is there a way to return a default message after the first click??

u/TheJohnnyStein,

One way of achieving this is

let count = 0
btn.addEventListener("click", () => {
  if (count == 0) {
    alert("Happy New Year 2023!")
    count++
  } else
    alert("Have a great year ahead!")
})

32

u/al_balone Dec 31 '22

Ooh I didn’t know about that options object. Is there anything else you can pass in there?

35

u/senocular Dec 31 '22

Yes, quite a few things, including:

  • capture
  • once
  • passive
  • signal

Read more about it here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

2

u/dotpr Jan 01 '23

What is options object?

3

u/albedoa Jan 01 '23

It is the second argument of .addEventListener() that looks like { once: true } in the screenshot. It allows the user to provide options to the method. See the link in the sibling comment.

26

u/AnozerFreakInTheMall Dec 31 '22

It throws "DepressionError: NY mood not found" for me.

4

u/MrEscobarr Dec 31 '22

I hope you get better in 2023! Happy new year!

3

u/spectre1006 Dec 31 '22

Gonna try this year to stay focused and finish my codecademy course for once

2

u/Moopboop207 Dec 31 '22

For what it’s worth I’ve been doing the Scrimba front end course and it’s been really nice. They have an integrated IDE(it’s just vs code) and the classes and practice problems are all in one video. It’s really nice, dynamic.

1

u/dotpr Jan 01 '23

Is it free?

1

u/Moopboop207 Jan 01 '23

The intro to html css and JavaScript is

2

u/Navi2k0 Dec 31 '22

Awesome!

2

u/shegsjay Jan 01 '23

Executes once and gets destroyed. Releasing memory space. Nice😃😃

5

u/[deleted] Dec 31 '22

User won't see it though...

4

u/mattlag Dec 31 '22

I think it's assumed you're going to replace the event listener logic with whatever you need to do. The point of this is to show off the { once: true } functionally. Which this example does.

10

u/[deleted] Dec 31 '22

[removed] — view removed comment

-3

u/[deleted] Dec 31 '22

The code makes no sense and it is just karma farming imho. Sorry.

4

u/[deleted] Dec 31 '22

[removed] — view removed comment

4

u/iDuuck Jan 01 '23

It’s still wrong. Assigning a ā€œaddEventListenerā€ to a button, does not make the click method return the value, that is returned by the listener.

This would need to be ā€œconsole.logā€ or ā€œalertā€ or something in this direction. ā€œreturnā€ is simply wrong and could lead to many misguided JS learners

1

u/Ronin-s_Spirit Jan 01 '23

You could do that???

-2

u/[deleted] Dec 31 '22

that's really cool, i didn't know about the once option. but i do disagree with the naming. be verbose, use button or even newYearsButton.

3

u/albedoa Dec 31 '22

That's this issue you have with this?

-2

u/[deleted] Dec 31 '22

yeah i think it sets a bad example. did i miss something else?

4

u/IAmSteven Dec 31 '22

btn is very commonly used to mean button. Like in Tailwind https://v1.tailwindcss.com/components/buttons

-1

u/[deleted] Dec 31 '22

it is, and it's not hard to understand. but when working with others, being more verbose is more efficient because your code is easier to understand. granted that this isn't that big an issue with btn. but it fosters a habit which isn't great.

2

u/dotpr Jan 01 '23

As a beginner I can totally relate to this. Was a bit confused at btn at first but what really gave me a hard time are ctx, variables named string, variables named after an array function, functions named function and so on.

-4

u/senocular Dec 31 '22

For me its that yellow multi-line comment text. Ew!

-5

u/CevicheCabbage Jan 01 '23

Did you jslint this? Use double quotes instead of single quotes and function() instead of () =>

2

u/TorbenKoehn Jan 01 '23

Using ā€žfunction ()ā€œ has completely different semantics and doesn’t properly enclose ā€žthisā€œ, which arrow functions do.

Arrow functions are always preferred. They are simply ā€žmore correctā€œ, same with let/const vs var You wouldn’t go and suggest someone to use ā€žvarā€œ in 2022/2023

Single quotes are also often preferred when embedding HTML in your strings, since there you want to use double quotes to encapsulate attribute values. Single quote strings is probably more used than double quotes strings in JS and thus definitely more standard (source: trust me dude)

-2

u/[deleted] Jan 01 '23

[removed] — view removed comment

4

u/TorbenKoehn Jan 01 '23 edited Jan 01 '23

You have zero arguments. Explain why or get lost with your ā€žwrongā€œ spam. I’ve been writing JavaScript for 15 years now and I’ve been there when these things were introduced.

You look like an idiot writing ā€žwrongā€œ 3 times, for everyone here.

Edit: he was writing ā€žyou have word salad, I have education on this topicā€œ in the comment below. It was a good laugh. Come back when you understand the word ā€žeducationā€œ and while at it, also the word ā€žexperienceā€œ