DEV Community

Cover image for JavaScript Tips in 5 Minutes! πŸš€
Marcos Silva for Webcrumbs

Posted on

JavaScript Tips in 5 Minutes! πŸš€

1. Use let and const Over var

πŸ”₯ Why?

  • Scope: let and const are block-scoped, making your code more predictable and avoiding those nasty bugs.
  • Hoisting: They don't suffer from the hoisting issues var does.
let score = 100; // Use for variables that will change
const pi = 3.14159; // Use for constants
Enter fullscreen mode Exit fullscreen mode

2. Master Arrow Functions

πŸ”₯ Why?

  • Concise: Cleaner and shorter syntax.
  • Lexical this: They do not bind their own this, which can be very handy.
const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5
Enter fullscreen mode Exit fullscreen mode

3. Template Literals for Better Strings

πŸ”₯ Why?

  • Readability: Easier to read and write strings, especially with variables.
  • Multi-line Strings: No need for awkward concatenation or escaping.
const name = 'Alice';
const greeting = `Hello, ${name}! Welcome to Wonderland.`;
console.log(greeting);
Enter fullscreen mode Exit fullscreen mode

4. Destructure for Simplicity

πŸ”₯ Why?

  • Clarity: Makes your code more readable and concise.
  • Efficiency: Pulls out exactly what you need from objects and arrays.
const user = { name: 'John', age: 30 };
const { name, age } = user;
console.log(name, age); // Output: John 30
Enter fullscreen mode Exit fullscreen mode

5. Use Default Parameters

πŸ”₯ Why?

  • Safety: Ensures functions have sensible default values.
  • Simplicity: Reduces the need for manual checks.
function greet(name = 'Stranger') {
  return `Hello, ${name}!`;
}
console.log(greet()); // Output: Hello, Stranger!
Enter fullscreen mode Exit fullscreen mode

6. Spread and Rest Operators

πŸ”₯ Why?

  • Versatility: Spread (...) and rest (...) operators simplify many tasks like copying arrays, merging objects, and handling function arguments.
// Spread
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // Output: [1, 2, 3, 4, 5, 6]

// Rest
function sum(...args) {
  return args.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3)); // Output: 6
Enter fullscreen mode Exit fullscreen mode

7. Short-Circuit Evaluation

πŸ”₯ Why?

  • Efficiency: Helps in writing concise and safer code, especially with default values.
const name = user && user.name;
const defaultValue = input || 'default';
Enter fullscreen mode Exit fullscreen mode

8. Optional Chaining

πŸ”₯ Why?

  • Safety: Avoids errors when accessing deeply nested properties.
const user = { address: { city: 'Paris' } };
const city = user?.address?.city;
console.log(city); // Output: Paris
Enter fullscreen mode Exit fullscreen mode

9. Avoid == and Use ===

πŸ”₯ Why?

  • Precision: === checks for both value and type, reducing unexpected behavior.
console.log(0 == false); // Output: true
console.log(0 === false); // Output: false
Enter fullscreen mode Exit fullscreen mode

10. Keep Your Code DRY

πŸ”₯ Why?

  • Maintainability: DRY (Don’t Repeat Yourself) principles make your code easier to maintain and understand.
function calculateTotal(price, tax) {
  return price * (1 + tax);
}

const item1Total = calculateTotal(100, 0.15);
const item2Total = calculateTotal(200, 0.15);
Enter fullscreen mode Exit fullscreen mode

Remember, the best way to learn is by doing. So, pick a tip, try it out, and watch your code transform! πŸ”„

Cheers! 🍻

Follow us: Webcrumbs
Webcrumbs

Top comments (0)