Stop Writing JavaScript Like This

Most of us are used to writing JavaScript code for a long time. But we might not have updated ourselves with new features which can solve your issues with minimal code. These techniques can help you write clean and optimized JavaScript Code. Today, I’l…


This content originally appeared on DEV Community and was authored by Ruben Gabrielyan

Most of us are used to writing JavaScript code for a long time. But we might not have updated ourselves with new features which can solve your issues with minimal code. These techniques can help you write clean and optimized JavaScript Code. Today, I’ll be summarizing some optimized JavaScript code snippets which can help you develop your skills.

1. Shorthand for if with multiple || conditions

if (fruit === 'apple' || fruit === 'orange' || fruit === 'banana' || fruit ==='grapes') {
    //code
}

Instead of using multiple || (OR) conditions, we can use an array with the values and use the includes() method.

if (['apple', 'orange', 'banana', 'grapes'].includes(fruit)) {
   //code
}

2. Shorthand for if with multiple && conditions

if(obj && obj.address && obj.address.postalCode) {
    console.log(obj.address.postalCode)
}

Use optional chaining (?.) to replace this snippet.

console.log(obj?.address?.postalCode);

3. Shorthand for null, undefined, and empty if checks

if (first !== null || first !== undefined || first !== '') {
    let second = first;
}

Instead of writing so many checks, we can write it better this way using ||
(OR) operator.

const second = first || '';

4. Shorthand for switch case

switch (number) {
  case 1:
     return 'one';
  case 2:
     return 'two';
  default:
     return;
}

Use a map/ object to write it in a cleaner way.

const data = {
  1: 'one',
  2: 'two'
};
//Access it using
data[num]

5. Shorthand for functions with a single line

function doubleOf(value) {
  return 2 * value;
}

Use the arrow function to shorten it.

const doubleOf = (value) => 2 * value

Buy me a coffee


This content originally appeared on DEV Community and was authored by Ruben Gabrielyan


Print Share Comment Cite Upload Translate Updates
APA

Ruben Gabrielyan | Sciencx (2021-10-14T14:53:16+00:00) Stop Writing JavaScript Like This. Retrieved from https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/

MLA
" » Stop Writing JavaScript Like This." Ruben Gabrielyan | Sciencx - Thursday October 14, 2021, https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/
HARVARD
Ruben Gabrielyan | Sciencx Thursday October 14, 2021 » Stop Writing JavaScript Like This., viewed ,<https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/>
VANCOUVER
Ruben Gabrielyan | Sciencx - » Stop Writing JavaScript Like This. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/
CHICAGO
" » Stop Writing JavaScript Like This." Ruben Gabrielyan | Sciencx - Accessed . https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/
IEEE
" » Stop Writing JavaScript Like This." Ruben Gabrielyan | Sciencx [Online]. Available: https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/. [Accessed: ]
rf:citation
» Stop Writing JavaScript Like This | Ruben Gabrielyan | Sciencx | https://www.scien.cx/2021/10/14/stop-writing-javascript-like-this/ |

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.