Why Learn JavaScript?

JavaScript is the undisputed scripting language of the web. It powers everything from lightweight browser animations and form validation to high-scale Node.js backend APIs and mobile applications built with React Native.


1. Variables and Scoping: let vs. const vs. var

Modern JavaScript (ES6+) introduced block-scoped variable declarations:

javascript
// const: Cannot be reassigned; use by default
const maxAttempts = 3;

// let: Block-scoped, reassignable; use when values change
let currentScore = 0;
currentScore += 10;

// Avoid 'var': Function-scoped, hoisted with undefined, causes subtle scope bugs

2. Functions & Arrow Syntax

Functions bundle reusable logic. Arrow functions provide concise syntax and lexical this binding:

javascript
// Traditional function declaration
function calculateGrade(score) {
  return score >= 80 ? 'Pass with Certificate' : 'Review Required';
}

// Arrow function expression
const calculatePercentage = (earned, total) => Math.round((earned / total) * 100);

console.log(calculateGrade(85)); // "Pass with Certificate"
console.log(calculatePercentage(16, 20)); // 80

3. Declarative Array Transformations

Rather than writing traditional for-loops, modern JavaScript emphasizes array methods:

javascript
const quizQuestions = [
  { id: 1, tech: 'javascript', points: 10 },
  { id: 2, tech: 'html', points: 5 },
  { id: 3, tech: 'javascript', points: 15 }
];

// .filter(): Select matching elements
const jsQuestions = quizQuestions.filter(q => q.tech === 'javascript');

// .map(): Transform each item
const pointValues = jsQuestions.map(q => q.points); // [10, 15]

// .reduce(): Aggregate into a single value
const totalJsPoints = pointValues.reduce((sum, p) => sum + p, 0); // 25

4. Asynchronous JavaScript: Promises & async/await

JavaScript runs on a single thread with an event loop. Network requests and timers execute asynchronously:

javascript
async function fetchUserProgress(userId) {
  try {
    const response = await fetch(`https://www.hangbug.com/api/users/${userId}`);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Failed to load user progress:', error.message);
    return null;
  }
}

Frequently Asked Questions

What is the difference between == and ===?

== performs loose equality with type coercion (e.g. 5 == '5' is true). === performs strict equality without coercion, checking both value and type (e.g. 5 === '5' is false). Always use ===.

What is a closure?

A closure is the combination of a function bundled together with references to its surrounding lexical environment. It allows an inner function to remember variables from an outer function even after the outer function has finished executing.


Test Your Knowledge