Appsberg
Back to Blogs
JavaScript code snippets on a screen
JavaScript

10 JavaScript Tips Every Developer Should Know

Discover lesser-known yet powerful JavaScript tricks that can boost your productivity and write cleaner code.

Priya KapoorNovember 15, 2024 · 8 min read
JavaScript
tips
web development
best practices

JavaScript has evolved significantly over the years, and there are many features and patterns that developers often overlook. In this post, we'll walk through ten practical tips that can make your code more efficient and maintainable.

1. Use Optional Chaining for Safe Property Access – Instead of nested conditionals, use `?.` to safely access nested properties. It short-circuits when a value is null or undefined.

2. Nullish Coalescing for Default Values – The `??` operator only falls back when the value is null or undefined, unlike `||` which treats falsy values like 0 or empty string as triggers.

3. Array Destructuring with Rest – Swapping variables, extracting first elements, or splitting arrays becomes trivial with destructuring and the rest operator.

4. Object.fromEntries – Convert arrays of key-value pairs back into objects. Great for transforming API responses or mapping over entries.

5. Template Literals for Dynamic Strings – Beyond interpolation, use tagged templates for sanitization, i18n, or custom formatting.

6. Array.from() for Mapping – Create arrays from iterables and apply a mapping function in one step. Perfect for DOM collections or converting Sets.

7. Logical Assignment Operators – `&&=`, `||=`, and `??=` combine assignment with conditional logic, reducing boilerplate.

8. Promise.allSettled – When you need all promises to complete regardless of success or failure, use `Promise.allSettled` instead of `Promise.all`.

9. Intl for Formatting – Use the Intl API for dates, numbers, and lists. It handles localization and formatting without external libraries.

10. Private Class Fields – Use `#` for truly private fields in classes. They're not accessible from outside the class or subclasses.

These tips can help you write more maintainable and performant JavaScript. Start with one or two that fit your current projects and gradually adopt them across your codebase.