DEV Community

Cover image for Mastering JavaScript Shorthand Techniques: Code Faster and Cleaner Part 2
ABIDULLAH786
ABIDULLAH786

Posted on • Edited on • Originally published at devwebbytes.blogspot.com

9 2 1 2

Mastering JavaScript Shorthand Techniques: Code Faster and Cleaner Part 2

This blog is originally published on my Blog website, where you can find the full version with detailed insights and examples. Click the link below to read the complete article and explore more tech-related content!
👉 Click Here

Introduction:

Writing clean and efficient code is essential for every JavaScript developer. Understanding and utilizing shorthand techniques can significantly enhance your productivity and make your code more expressive. In this blog, we will explore some powerful shorthand techniques that will simplify your JavaScript code and make it more concise. Let's dive in!

1. Arrow Functions

Replace traditional function declarations with arrow functions to write more concise and expressive code.

// Longhand
function sayHello(name) {
  console.log('Hello', name);
}

// Shorthand
const sayHello = name => console.log('Hello', name);
Enter fullscreen mode Exit fullscreen mode

2. Default Parameter Values

ES6 introduced default parameter values for functions, allowing you to provide fallback values if an argument is not passed or is undefined.

// Longhand
function greet(name) {
  if (name === undefined) {
    name = 'Guest';
  }
  console.log('Hello', name);
}

// Shorthand
const greet = (name = 'Guest') => console.log('Hello', name);
Enter fullscreen mode Exit fullscreen mode

3. Short-Circuit Evaluation

Use logical operators for concise conditional statements to check for existence before accessing properties or calling functions.

// Longhand
if (options && options.size && options.color) {
  doSomething();
}

// Shorthand
options?.size?.color && doSomething();
Enter fullscreen mode Exit fullscreen mode

4. Optional Chaining

The optional chaining operator (?.) allows you to access nested properties of an object without worrying about possible null or undefined values.

// Longhand
const street = user && user.address && user.address.street;

// Shorthand
const street = user?.address?.street;
Enter fullscreen mode Exit fullscreen mode

5. Nullish Coalescing Operator

The nullish coalescing operator (??) allows you to provide a default value when encountering null or undefined.

// Longhand
const value = someValue !== null && someValue !== undefined ? someValue : defaultValue;

// Shorthand
const value = someValue ?? defaultValue;
Enter fullscreen mode Exit fullscreen mode

6. Logical Assignment Operators

Logical assignment operators (&&=, ||=) combine logical operators with assignment in a concise way.

// Longhand
if (!enabled) {
  enabled = true;
}

// Shorthand
enabled ||= true;
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Mastering shorthand techniques in JavaScript can greatly improve your coding efficiency and readability. By using these concise syntaxes, you can write cleaner and more expressive code while reducing unnecessary repetition. Incorporate these shorthand techniques into your coding practices to become a more proficient and effective JavaScript developer.

Happy coding!

Feel Free to comment your thoughts regarding the topic

Feel Free to comment

Connect with me on Twitter, Linkedinand GitHub to stay updated and join the discussion!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay