Mastering JavaScript Shorthands

Imaad Uddin
CodeX
Published in
3 min readJan 10, 2024

There’s always a faster way!

Photo by KOBU Agency on Unsplash

Mastering JavaScript Shorthands: Tips for Beginners

As a developer, you know the importance of writing clean, concise, and efficient code. One way to achieve this is by using JavaScript shorthands. JavaScript shorthands are a set of concise syntaxes that allow you to write more readable and less verbose code. They are useful in everyday programming and are a must-have in every developer’s toolbox. In this article, we will cover some of the most useful shorthands that every developer should know.

The Ternary Operator

The ternary operator is a shorthand for the if-else statement. It’s often used to assign a value to a variable based on a condition. The ternary operator is written in the form of condition ? trueValue : falseValue. The condition is evaluated first, and if it's true, the trueValue is returned. If it's false, the falseValue is returned.

The ternary operator is especially useful when you need to assign a simple value to a variable based on a condition. You can use ternary operators to assign values based on conditions like checking if a number is even or odd:

Ternary Operator Code Snippet

The Nullish Coalescing Operator

The nullish coalescing operator is a shorthand for checking if a value is null or undefined, and returning a default value if it is. The nullish coalescing operator is written in the form of value ?? defaultValue. If value is null or undefined, it returns defaultValue. Otherwise, it returns value.

The nullish coalescing operator is particularly useful when you need to assign a default value to a variable if the original value is null or undefined. You can use the nullish coalescing operator to assign default values to variables like this:

Nullish Coalescing Operator Code Snippet

Optional Chaining

Optional chaining is a shorthand for checking if a property exists before accessing it. It’s particularly useful when working with nested objects. Optional chaining is written in the form of object?.property. If object is null or undefined, it returns undefined. Otherwise, it checks if property exists and returns its value.

Optional chaining is especially useful when you need to access a property of an object that may or may not exist. You can use optional chaining to access a nested property of an object like this:

Optional Chaining Code Snippet

Spread Syntax

The spread syntax is a shorthand for expanding an iterable object into individual elements. It’s particularly useful when you need to combine arrays or objects.

The spread syntax is especially useful when you need to concatenate two or more arrays. You can use the spread syntax to concatenate two arrays like this:

Spread Syntax Code Snippet

Arrow Functions

Arrow functions are a shorthand for writing anonymous functions. They’re particularly useful when you need to pass a function as an argument to another function.

Arrow functions are especially useful when you need to define a simple function that takes one or more arguments. You can use arrow functions to define a function that calculates the area of a rectangle like this:

Arrow Functions Code Snippet

In conclusion, JavaScript shorthands can make your code more concise, readable, and efficient. By mastering these shorthands, you can become a more effective developer. These are just a few of the shorthands available in JavaScript, so keep learning and practicing to become a better developer.

--

--

Imaad Uddin
CodeX
Writer for

Tech, Finance & Software Engineering || Connect With Me Here 👉🏽 https://solo.to/imaaduddin