Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Everyone has twenty-four hours a day.

We cannot create more time than that number but we can use it efficiently every single second.

One way to do this is to shorten your code with the following 15 techniques.

1. Declaring Variables Shorthand

If it does not make sense, don’t declare each variable on every single line. One line is enough, save your spaces.

Instead of:

let name;
let price = 12;
let title;
let discount = 0.3;

Do this:

let name, price = 12, title, discount = 0.3;

2. Condition Shorthand

I think this is so obvious but as long as it serves the subject, I put it in.

Instead of:

if (isUsernameValid === true) {}

if (isExist === false) {}

Do this:

if (isUsernameValid) {}

if (!isExist) {}

3. Using Decimal Base Exponent

1000000 can cause some bugs if you miss one or two zero. The more zero in the tail, the more attention you have to pay. Fortunately, you don’t have to do that. Instead of writing 1000000, you can just shorten it to 1e6. The 6 represents the number of zeros in the tail.

Instead of:

let length = 10000;
for (let i = 0; i < length; i++) {}

Do this:

let length = 1e4;
for (let i = 0; i < length; i++) {}

4. Default Parameters

Sometimes you have to define a function with multiple parameters. Do you need to pass all the parameters values every time you invoke the function? Not really if you initialize the default values.

Instead of:

let generateBookObject = (name, price, discount, genre) => {
  let book = { name, price, discount, genre };
  return book;
};
let book = generateBookObject(‘JavaScript’, 12, 0.3, ‘Technology’); // { name: ‘JavaScript’, price: 12, discount: 0.3, genre: ‘Technology’ }

Do this:

let generateBookObject = (name = ‘JavaScript’, price = 12, discount = 0.5, genre = ‘Technology’) => {
  let book = { name, price, discount, genre };
  return book;
};
// In case discount and genre are the same as the default values, you don’t need to pass them to the function
let book = generateBookObject(‘JavaScript’, 12); // { name: ‘JavaScript’, price: 12, discount: 0.5, genre: ‘Technology’ }

5. Ternary Operator

If there’s only one else in the conditional statement, shrink it to one line using ternary.

Instead of:

let name = ‘Amy’;
let message;

if (name === ‘Amy’) {
  message = ‘Welcome back Amy’;
} else {
  message = ‘Who are you?’;
}

Do this:

let name = ‘Amy’;
let message = name === ‘Amy’ ? ‘Welcome back Amy’ : ‘Who are you?’;

programming coding javascript javascript-tips

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

7 Simple JavaScript Tips for Optimizing Your Code

You may not know about these tips 7 Simple JavaScript Tips for Optimizing Your Code

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

How to start writing less error-prone code in JavaScript

How to start writing less error-prone code in JavaScript - Everything in JavaScript is an object!’. We said that this assertion is false. Many things in JavaScript can behave like an object, but that doesn’t mean it’s the object. We can say we have three types of objects (objects, functions and arrays) in JavaScript.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!