DEV Community

Discussion on: The Most POWERFUL [JavaScript] Function

Collapse
 
insidewhy profile image
insidewhy • Edited

For the join case, you can do it much more simply since if you omit the first value, then the first entry in the array is used (reduce is even more powerful than you realise 😛). For some you don't need the ternary or ===, you can just use condition || ....

Collapse
 
cleancodestudio profile image
Clean Code Studio

Hey @insidewhy , thanks for taking the time to drop your coding tips for reduce. Can you show me a quick coding example of what you mean by omitting the first value and automatically using the first entry in the array?

I'm familiar with using || instead of the ternary operator. I usually opt for the ternary operator because it provides better debugging support by default when errors arise.

That first tip you dropped sounds interesting though, I'd love to see an example of what you're specifically referring to!

Collapse
 
insidewhy profile image
insidewhy • Edited

I usually opt for the ternary operator because it provides better debugging support by default when errors arise.

What debugger are you using? I've never seen this. You always prefer ternary over logical operators because of your debugger? I'm a little scared now. If you're willing to accept code verbosity because of this belief then I hope you're not mistaken.

In situations where one side of the operator is a boolean, I don't see how there can be any ambiguity.

Thread Thread
 
cleancodestudio profile image
Clean Code Studio

Hey insidewhy, I went back and tried to replicate the situation that inspired me to begin using the ternary instead of or.

I wasn't able to replicate it, so I'm thinking it may have been something to do with how ternary/or operators work within the scope of a reactive front-end framework, or how ternary/or operators work on undefined objects (Ex: person.address.street_t - if address doesn't exist), or javascript has simply been updated to correct the issue.

I appreciate you bringing this up in the comments though, I'm headed off for the day - but once I get back on I'm going to try to see if I'm missing something.

If I find something I'll let you know and if I don't then I owe you a big thanks because your boy is about to be using or operators instead of ternaries again :)

Here's to not finding whatever had me spooked when it comes to the or operator

Collapse
 
val_baca profile image
Valentin Baca • Edited

You can just do:

['truck', 'car', 'people'].reduce((text, word) => `${text}-${word}`)

> "truck-car-people"
Enter fullscreen mode Exit fullscreen mode

If no initial variable is given (your first example had '') then it just pulls from the input, in this case, 'truck'

You (obviously) need an initial value if the array is empty or you get a very descriptive error:

[3, 2.1, 5, 8].reduce((total, number) => total + number, 0)
> 18.1

[3, 2.1, 5, 8].reduce((total, number) => total + number)
> 18.1

[].reduce((total, number) => total + number, 0)
> 0

[].reduce((total, number) => total + number)
> VM158:1 Uncaught TypeError: Reduce of empty array with no initial value

Enter fullscreen mode Exit fullscreen mode