Thuta Learning
IntermediateProgrammingbeginner

Best Practices

Relax. We'll talk through this in plain words — no textbook voice.

JavaScript Best Practices are guidelines for writing clean, maintainable code.

✨ Best Practices:

• Use const/let instead of var

• Use strict mode ('use strict')

• Meaningful variable names

• Avoid global variables

• Use === instead of ==

• Handle errors properly

• Comment complex code

• Use modern ES6+ features

⚠️ Common Pitfalls:

• Forgetting 'this' context

• Callback hell

• Not handling async errors

• Memory leaks

javascript
'use strict';

// Good practices

// 1. Use const/let, not var
const MAX_USERS = 100;
let currentUsers = 0;

// 2. Use === for comparison
if (currentUsers === 0) {
    console.log("No users");
}

// 3. Meaningful names
function calculateTotalPrice(items) {
    return items.reduce((sum, item) => sum + item.price, 0);
}

const items = [{price: 10}, {price: 20}];
console.log(`Total: ${calculateTotalPrice(items)}`);

// 4. Arrow functions for callbacks
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log(`Doubled: ${doubled}`);

// 5. Template literals
const name = "Aung Kyaw";
const age = 25;
console.log(`${name} is ${age} years old`);

// 6. Default parameters
function greet(name = "Guest") {
    return `Hello, ${name}!`;
}
console.log(greet());
You should see
No users Total: 30 Doubled: [2, 4, 6, 8, 10] Aung Kyaw is 25 years old Hello, Guest!
Best Practices | Thuta Learning