DEV Community

Cover image for Awesome JavaScript hacks

Awesome JavaScript hacks

Mitchell Mutandah on December 21, 2022

In this post I will share useful hacks for JavaScript. These hacks reduce the code and will help you to run optimized code. So letโ€™s start hacking!...
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

A few things:

  • None of these are 'hacks' - they're just features of the language
  • Your typeof example is missing typeof - oops! ๐Ÿ˜
  • Your shuffling example will give biased results (some shuffle permutations will turn up way more often than others - explanation here)
  • Your method for testing if an object has values is overly simplistic, and may result in you deleting objects that are, in fact, not empty. It will not include keys that are Symbols, string keys that are non-enumerable, and values coming from lower down in the prototype chain. For more information on this, you can look here
  • You can add syntax highlighting to your examples by adding js after the opening three backticks of the code block
Collapse
 
wadecodez profile image
Wade Zimmerman

Came here to say the same thing. "Hacks" are more of a thing for working around libraries/frameworks. Like using reflection to access a private property or something. Better title would have been something like "My favorite pieces of code"

Collapse
 
mitchiemt11 profile image
Mitchell Mutandah

Thank you Jon Randy for highlighting some crucial points. I have updated the typeof function. There is more to learn. Thanks again for the useful links.

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Might want to update it again, since typeof is an operator, not a function (as you initially correctly state) - and the data you are checking is the operand, not an argument to a function. For this reason, the parentheses in your example are unnecessary.

Collapse
 
pilcrowonpaper profile image
pilcrowOnPaper

Please don't use && as a shortcut for conditional statements. It is harder to read and understand. You may want to use ?? instead of || if you just want to check if the value is undefined or null.

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

Readability and understand-ability are purely subjective.

Collapse
 
mhasan profile image
Mahmudul Hasan • Edited

Good post, learn this things. Please share more!

Collapse
 
mitchiemt11 profile image
Mitchell Mutandah

Thank you.

Collapse
 
fruntend profile image
fruntend

ะกongratulations ๐Ÿฅณ! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up ๐Ÿ‘

Collapse
 
not-ethan profile image
Ethan

Why did you only mention console.table when there are many other methods for the console like console.group, console.error, console.warn` and many other super useful methods on the console object?

Collapse
 
amirhe profile image
Amir.H Ebrahimi

Noteing something for readers, as article said typeof is an operator not a function

so it can also be used like an operator without prantesis.

const isSSR = () => typeof window === 'undefiend';
Enter fullscreen mode Exit fullscreen mode

value wrapped with prantesis is going to be evaluated to variable iteself.

Collapse
 
mitchiemt11 profile image
Mitchell Mutandah

Thanks, updated.

Collapse
 
pavelee profile image
Paweล‚ Ciosek

Thanks!

Collapse
 
dil2010 profile image
Dilan Udawattha

Wow!, Good to know

Collapse
 
_ianeta profile image
Ianeta Hutchinson • Edited

We could break this down into bite sized bits for other sharing formats