DEV Community

Discussion on: 10 Practical JavaScript Tricks

Collapse
 
david profile image
David Hunt

Eh, I hate to be a jerk, but this is full of really poor practices. To be honest, I stopped after 2 or 3 because they're teaching bad lessons.

Examples:

  1. arguments should be deprecated in favor of rest parameters.
  2. Your reduce function is missing a starting value, a 0 in this case as the second argument to reduce.
  3. Mutating function arguments.
  4. (and others) relying on side effects of ECMAScript APIs instead of using reliable APIs.

Please, beginners reading through this, don't do most of these things.

Collapse
 
yevhene profile image
Yevhen Shemet
  1. Reduce will call function with two first arguments. But you are right, author please never use 1 and 6
Collapse
 
bdougherty profile image
Brad Dougherty

Actually, leaving out the starting value is fine as long as your array is not empty. When you do that, it will use the first element as the starting value. I think in general it is better to be explicit and provide one though.

Collapse
 
zandershirley profile image
Zander Shirley

I appreciate that you took the time to write a comment.

You remind me of a friend that has been waiting for 2 years to get some experience in order to give advice to beginners.

He just keeps giving strange advice and the junior devs simply ignore him.

  1. arguments is not deprecated.

The MDN webdocs just provide the following note:
If you're writing ES6 compatible code, then rest parameters should be preferred.

I never said that you should write all of your code with the tricks that I used, they're just presented as an interesting read.

It seems you just missed the point completely.

Collapse
 
dmalechek profile image
dmalechek

Nice tips for any of those contests to do things in the few number of lines, but I'm with David Hunt, this is almost a must set of error lint rules. In enterprise apps, never sacrifice readability for fewer lines of code without a very, very good reason.

If you don't like his comments, I might say why did the world need another "10 javascript..." repeat article?

Collapse
 
ikoshelev profile image
IKoshelev

"You remind me of a friend that has been waiting for 2 years to get some experience in order to give advice to beginners."

Your friend is right. When I see advice like the ones in the article, I usually say "I hope your doctor and your lawyer are better at their job than this."

I've handled over a 100 front-end interviews for my employers and code like that would seriously hurt candidates chances.

Thread Thread
 
zandershirley profile image
Zander Shirley

It's pretty obvious that these tricks do not make your JavaScript more readable, they're not supposed to help with readability.

The purpose is to show the reader that things in JavaScript can also work this way.

I'm sure you're an expert in the bleeding edge technology that your employer is using and you're really good at those interviews, but it seems you just missed the point of this article.

Thread Thread
 
ikoshelev profile image
IKoshelev • Edited

merriam-webster.com/dictionary/pra...
Practical:

  • "of, relating to, or manifested in practice or action : not theoretical or ideal"
  • " capable of being put to use or account"

"The purpose is to show the reader that things in JavaScript can also work this way." - even you yourself describe this precisely as theoretical. Practical is "here is how this should be done".

Look, I would not be commenting here, if I haven't seen over and over again junior devs reading such articles and rushing to use tricks from them everywhere. The best course of action for you would be to remove half of the tricks you've written, since only harm can come out of them. The question is - are you willing to admit you were wrong or is your ego more important to you than your readers?

Collapse
 
oli8 profile image
Olivier

Nothing wrong with the reduce here.

Collapse
 
clydegrey profile image
Clyde

You can skip the initial value in your reduce, but now you have to check that the array length is greater than zero.

So you've introduced a serious bug in your application to save typing two characters.

Also as others have pointed out use of var is discouraged, use of arguments keyword is discouraged and a few other bad practices.