JavaScript’s Shorthand Syntax That Every Developer Should Know

Skylar Johnson
2 min readAug 2, 2023

--

JavaScript is a versatile and powerful programming language, and developers are constantly finding ways to write code more efficiently and accurately. Shorthand syntax is one such technique that allows developers to simplify code and improve readability without sacrificing functionality. In this article, we look at some common shorthand syntax techniques in JavaScript that every developer should know. These techniques will help you write cleaner, more meaningful code and make you a more effective JavaScript developer.

Object Literal Shorthand

When creating an object, you can use a shortcut to variable names and object keys have the same name. Instead of explicitly specifying the key and value, you can omit the key and specify only the value Here’s an example:

// Longhand syntax
const name = "John";
const age = 30;
const person = { name: name, age: age };

// Shorthand syntax
const name = "John";
const age = 30;
const person = { name, age };

Destructuring Assignment

Destructuring allows you to precisely extract individual values ​​from arrays or object properties. This avoids boilerplate code and makes it easier to manage complex data structures. Consider the following example:

// Longhand syntax
const person = { name: "John", age: 30 };
const name = person.name;
const age = person.age;

// Shorthand syntax
const person = { name: "John", age: 30 };
const { name, age } = person;

Ternary Operator

The ternary operator is a compact way to write if- statements else simple. Evaluates an expression and returns one of two values, depending on whether the condition is true or false.

// Longhand syntax
let isAuthenticated = false;
let message;

if (isAuthenticated) {
message = "Welcome back!";
} else {
message = "Please log in.";
}

// Shorthand syntax
let isAuthenticated = false;
let message = isAuthenticated ? "Welcome back!" : "Please log in.";

Short-circuit Evaluation

Short-circuit evaluation is a technique where the evaluation of an expression stops as soon as it exists. A result can be determined. Often used with logical operators to provide default or process values ​​of potentially undefined variables.

// Longhand syntax
const settings = {
isEnabled: true,
debugMode: false,
};

const mode = settings.debugMode === true ? "debug" : "normal";

// Shorthand syntax
const settings = {
isEnabled: true,
debugMode: false,
};

const mode = settings.debugMode && "debug" || "normal";

Arrow Functions

Arrow functions provide a concise syntax for writing function expressions. They implicitly return a single expression without requiring the returnkeyword and automatically bind the value of that expression to the surrounding context.

// Longhand syntax
function add(a, b) {
return a + b;
}

// Shorthand syntax
const add = (a, b) => a + b;

JavaScript shorthand syntax can greatly improve the readability of your code and reduce unnecessary verbosity. By understanding and using these binding techniques, you can become a more efficient and effective JavaScript developer. Remember that while these shortcuts are useful, it’s just as important to keep your code clear and consistent. Therefore, use shorthand syntax wisely and ensure that your code remains easy to understand for you and other developers working on the project.
Happy coding!

--

--

Skylar Johnson
Skylar Johnson

Written by Skylar Johnson

I'm a Web developer who is always looking to learn more and eat too much chocolate. https://www.thetravelocity.com

No responses yet