Member-only story
7 Shorthand Optimization Tricks every JavaScript Developer Should Know π
Every language has its own quirks and JavaScript, the most used programming language, is no exception. This article will cover a plethora of JavaScript Shorthand Optimization tricks that can help you write better code, and also make sure this is NOT your reaction when you encounter them:
1. Multiple string checks
Often you might need to check if a string
is equal to one of the multiple values, and can become tiring extremely quickly. Luckily, JavaScript has a built-in method to help you with this.
// Long-hand
const isVowel = (letter) => {
if (
letter === "a" ||
letter === "e" ||
letter === "i" ||
letter === "o" ||
letter === "u"
) {
return true;
}
return false;
};// Short-hand
const isVowel = (letter) =>
["a", "e", "i", "o", "u"].includes(letter);
2. For-of
and For-in
loops
For-of
and For-in
loops are a great way to iterate over an array
or object
without having to manually keep track of the index of the keys
of the object
.