Important JavaScript Shorthands to know 🚀🔥

Arjun Vijay Prakash - May 25 '22 - - Dev Community

1) The Ternary Operator

Longhand-

const x = 20;
let answer;
if (x > 10) {
    answer = "greater than 10";
} else {
    answer =  "less than 10";
}
Enter fullscreen mode Exit fullscreen mode

Shorthand

const answer = x > 10 ? "greater than 10" : "less than 10";
Enter fullscreen mode Exit fullscreen mode

2) Declaring Variables Shorthand

Longhand-

let x;
let y;
let z = 3;
Enter fullscreen mode Exit fullscreen mode

Shorthand

let x, y, z=3;
Enter fullscreen mode Exit fullscreen mode

3) If True Presence Shorthand

Longhand-

if (likeJavaScript === true)
Enter fullscreen mode Exit fullscreen mode

Shorthand

if (likeJavaScript)
Enter fullscreen mode Exit fullscreen mode

4) If Not True Presence Shorthand

Longhand-

if (!likeJavaScript === true)
Enter fullscreen mode Exit fullscreen mode

Shorthand

if (!likeJavaScript)
Enter fullscreen mode Exit fullscreen mode

5) Arrow Function Shorthand

Longhand-

function doAnything(){}
Enter fullscreen mode Exit fullscreen mode

Shorthand

let doAnything = () => {}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .