In the constantly-changing JavaScript ecosystem, designing simple and efficient code is more important than ever. The release of ES6 introduced a slew of new capabilities that simplify and streamline the way we write JavaScript. This article discusses various new ES6 approaches that can replace older, more verbose patterns, resulting in cleaner and more readable code. From Boolean casting to the powerful spread operator, these strategies increase code readability, efficiency, and maintainability. Let’s look at some of the essential techniques that any JavaScript writer should follow to build minimum ES6 code.
1. Boolean Casting
Today’s recommended method according to Airbnb’s style guide
2. Nullish Coalescing
Returns its right-hand side when its left-hand side operand is null or undefined
const addId = (user, id) => {
user.id =
id !== null && id !== undefined
? id
: “Unknown“
return user
}
const addId = (user, id) => {
user.id = id ?? “Unknown“
return user
}
3. Default Parameters
Description: Function Parameters default to undefined, so it’s useful to set a value for this eventuality.
const createUser = (name, email) => {
const user = {
email,
name: name ?? “Unknown“,
}
// create user
}
const createUser = (
name = “Unknown“,
) => {
const user = { email, name }
// create user
}
4. Optional Chaining
Description: Allows you to read the value of a deeply nested property without checking if it’s a valid chain.
const hasValidPostcode = u =>
u &&
u.address &&
u.address.postcode &&
u.address.postcode.valid
const hasValidPostcode = u => u?.address?.postcode?.valid
5. Destructuring Objects
Description: Write less code by unpacking properties from objects into distinct variables.
const save = params => {
saveData(
params.name,
params.email,
params.dob
)
}
const save = ({name, email, dob}) => {
saveData(name, email, dob)
}
6. Destructuring Arrays
Description: Write less code by unpacking values from arrays into distinct variables.
const data = [
[“axios“, “recharts“],
[“flocked“, “flick“]
]
const plugins = data[0], apps = data[1]
const data = [
[“axios“, “recharts“],
[“flocked“, “flick“]
]
const [plugins, apps] = data
7. Spread Operator
Description: Merge two objects into one using this cool syntax, also very clean when coding objects.
const details = {name: “Man Utd“}
const stats = {games: 7, points: 21}
const team = Object.assign(
{},
details,
stats
)
const details = {name: “Man Utd“}
const stats = {games: 7, points: 21}
const team = {
…details,
…stats
}
8. For(of)
Description: Arguably the same amount of code required but for(of) is known to be 24% faster than forEach.
const array = []
const fillArray = items => {
items.forEach(i => array.push(i))
}
const array = []
const fillArray = items => {
for (let i of items) {
array.push(i)
}
}
Conclusion
To summarize, using ES6 features may significantly simplify your JavaScript code, making it more brief, legible, and efficient. Integrating these current methods will result in cleaner and more maintainable code, improving both development and performance. Implement these approaches to improve your code standards and simplify your tasks.
Happy Coding 👨💻