This content originally appeared on DEV Community and was authored by Md Shijan Ali
Hey devs! Letβs talk JavaScriptβthe language we love (and sometimes hate ). Here are 5 quick tips to write cleaner, more efficient code:
1. Destructuring Magic
No more object.property
chains!
javascript
const user = { name: 'Dev', age: 25, hobby: 'coding' };
const { name, age } = user; // Boom! Clean.
2. Optional Chaining (?.)
Avoid Cannot read property 'x' of undefined
errors:
javascript
const city = user?.address?.city || 'Not provided';
3. Template Literals
Ditch ugly string concatenation:
javascript
console.log(`Hello, ${name}! Youβre ${age}? Awesome!`);
4. Arrow Functions + Implicit Return
Shorter syntax for small functions:
javascript
const double = num => num * 2; // 😍
5. async/await
Error Handling
Wrap it in a try-catch without .then()
hell:
javascript
try {
const data = await fetchAPI();
} catch (err) {
console.error("Oops!", err);
}
Discussion Time!
Which tip did you find most useful?
Got any other JS pro-tips? Drop them below!
P.S. Follow me for more web dev nuggets!
This content originally appeared on DEV Community and was authored by Md Shijan Ali