20 JavaScript Shorthand Techniques that will save your time - JS Curious

20 JavaScript Shorthand Techniques that will save your time - JS Curious

The shorthand techniques of any programming language help you to write more clean and optimized code which improves readability.

1. Declaring variables

let x;
let y = 20;
​
//Shorthand
let x, y = 20;

2. Assigning values to multiple variables

We can assign values to multiple variables in one line with array destructuring.

let a, b, c;
​
a = 5;
b = 8;
c = 12;
​
//Shorthand
let [a, b, c] = [5, 8, 12];

3. The Ternary operator

We can save 5 lines of code here with ternary (conditional) operator.

let marks = 26; 
let result; 
if(marks >= 30){
   result = 'Pass'; 
}else{ 
   result = 'Fail'; 
} 
​
//Shorthand 
let result = marks >= 30 ? 'Pass' : 'Fail';

4. Assigning default value

We can use OR(||) short circuit evaluation to assign a default value to a variable in case the expected value found empty.

let imagePath;
​
let path = getImagePath();
​
if(path !== null && path !== undefined && path !== '') {
    imagePath = path;
} else {
    imagePath = 'default.jpg';
}
​
//Shorthand
let imagePath = getImagePath() || 'default.jpg';

5. AND(&&) Short circuit evaluation

If you are calling a function only if a variable is true, then using AND(&&) short circuit you can do it in a single line.

if (isLoggedin) {
    goToHomepage();
 }
​
//Shorthand
isLoggedin && goToHomepage();

Here in shorthand technique, if isLoggedin returns true, then only goToHomepage() will execute.

Read the original post in jscurious.com

javascript shorthand web-development typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.