Some cool JavaScript Shorthand that will make your code cleaner than your peer’s

Here are 7 JavaScript shorthands that you as a JavaScript developer should know because who doesn’t like shorter and cleaner code ¯_(ツ)_/¯

Convert string to number

Normally we use the parseInt() to do this. But, you can do this using the un…


This content originally appeared on DEV Community and was authored by Shubhra Agarwal

Here are 7 JavaScript shorthands that you as a JavaScript developer should know because who doesn't like shorter and cleaner code ¯_(ツ)_/¯

Convert string to number

Normally we use the parseInt() to do this. But, you can do this using the unary operator +. Sick right?
image

You can also do this by just adding the unary operator to an empty string
image

The ternary operator

I'm pretty sure all of you must be aware about this but what's the harm in sharing it again.

image

The short circuit

Ever wanted to check for a condition and output something only if the condition was true? Were you also using && like me? Well, not anymore, I present to you the short circuit.
image
image

Flattening an array

A lot of people use various methods to flatten an array like filter(), concat etc. But using the flat() method can get the job done quicker and better.
image

Merging arrays

Merging of arrays is one of the tasks that we need to do in our day to day coding. Be it the data from an API or whatever. Using the spread operator can get this job done in no time.
image

Cloning Arrays

Just like merging, spread operator comes in handy even when you want to clone an array

image

The shorthand of for loops

We all have been writing for loops the C++ way up until now, but now it's time to do it the modern way. image

TL;DR

  1. Convert string to number
  2. Ternary Operator
  3. Short Circuit
  4. Flattening an array
  5. Merging arrays
  6. Cloning Arrays
  7. The shorthand for loop


This content originally appeared on DEV Community and was authored by Shubhra Agarwal


Print Share Comment Cite Upload Translate Updates
APA

Shubhra Agarwal | Sciencx (2021-08-16T10:35:30+00:00) Some cool JavaScript Shorthand that will make your code cleaner than your peer’s. Retrieved from https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/

MLA
" » Some cool JavaScript Shorthand that will make your code cleaner than your peer’s." Shubhra Agarwal | Sciencx - Monday August 16, 2021, https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/
HARVARD
Shubhra Agarwal | Sciencx Monday August 16, 2021 » Some cool JavaScript Shorthand that will make your code cleaner than your peer’s., viewed ,<https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/>
VANCOUVER
Shubhra Agarwal | Sciencx - » Some cool JavaScript Shorthand that will make your code cleaner than your peer’s. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/
CHICAGO
" » Some cool JavaScript Shorthand that will make your code cleaner than your peer’s." Shubhra Agarwal | Sciencx - Accessed . https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/
IEEE
" » Some cool JavaScript Shorthand that will make your code cleaner than your peer’s." Shubhra Agarwal | Sciencx [Online]. Available: https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/. [Accessed: ]
rf:citation
» Some cool JavaScript Shorthand that will make your code cleaner than your peer’s | Shubhra Agarwal | Sciencx | https://www.scien.cx/2021/08/16/some-cool-javascript-shorthand-that-will-make-your-code-cleaner-than-your-peers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.