Digital Garden

JavaScript

Core JavaScript concepts and patterns

JavaScript Fundamentals

JavaScript is a versatile programming language that powers the web. Here are the core concepts you need to know.

Variables and Data Types

JavaScript has three ways to declare variables:

var oldWay = "avoid this";
let mutableValue = "can be reassigned";
const immutableBinding = "cannot be reassigned";

Functions

Functions are first-class citizens in JavaScript:

// Function declaration
function greet(name) {
  return `Hello, ${name}!`;
}

// Arrow function
const greetArrow = (name) => `Hello, ${name}!`;

// Higher-order function
const withLogging =
  (fn) =>
  (...args) => {
    console.log("Calling function with:", args);
    return fn(...args);
  };

Promises and Async/Await

Modern JavaScript uses Promises for asynchronous operations:

// Promise-based
fetch("/api/data")
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));

// Async/await
async function fetchData() {
  try {
    const response = await fetch("/api/data");
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

Modules

ES Modules are the standard for organizing JavaScript code:

// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

// main.js
import { add, subtract } from "./math.js";