JavaScript Tips in 5 Minutes! ๐Ÿš€

m4rcxs

Marcos Silva

Posted on July 30, 2024

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

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
m4rcxs
Marcos Silva

Posted on July 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About