JavaScript ShortHand

This is really needed for all javascript developers who want to save their time for writing better code with efficiency way. Here I will guide you on shorthand techniques of javascript.

Declaring Variables

Perfect variable declaration is the good practice when you declare the variable on top of the function and inner function. It will save you a lot of time.

// Longhand:
let a;
let b = 1;
let c = 'active';
// Shorthand:
let a, b = 1, c = 'active';

Multiple String Variables

Sometimes we need to add multiple variables one by one with string. Most of the cases we just simple use (+) to adding a variable with the string. Now we will use Template Literals to adding variable with a string that will make it easier to write the code by using backtick and ${}.

const roomName = "Operation Theatre";
const providerName = "Shohel Rana";
// Longhand
const warningMessage = "Your room[" + roomName + "] is not available, Please contact your provider[" + providerName + "]";
// Shorthand
const warningMessage = `Your room[${roomName}] is not available, Please contact your provider[${providerName}]`;

Assigning a variable value to another variable

When you may assign a variable value to another variable, you may want to ensure that the source variable is not null, undefined, or empty. You can either write a long if statement with multiple conditionals or shorthand techniques.

let variable1;
let variable2;
// Longhand
if (variable1 !== null || variable1 !== undefined || variable1 !== "") {
let variable2 = variable1;
}
// Shorthand
variable2 = variable1 || "empty or other value";

If Presence

As a programmer sometimes we need to check if the variable is present or not. This is the shorthand to check your variable presence.

// Longhand
if (variable1 === true)
// Shorthand
if (variable1)

Multi-line String

Using the backtick you can write the multiple-line string easily.

// Longhand
const msg ="When I'm not in front of a computer screen" +
"of my entire soul, like these sweet mornings of spring\n\t" +
"or focusing on setting myself up for achieving,\n\t" +
"and feel the charm of existence in this spot,\n\t" +
"up for achieving a leadership role in any.\n\t ";
// Shorthand
const msg = `When I'm not in front of a computer screen, I'm probably busy with doing some research on AI stuffs or focusing on setting myself up for achieving a leadership role in any challenging and creative environment.`;

--

--

Angular | React | .NET | Java | C# | NLP | ML | AI | Technology Enthusiast. www.shohel.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shohel Rana

Angular | React | .NET | Java | C# | NLP | ML | AI | Technology Enthusiast. www.shohel.net