javascript
console.log(value);Core JavaScript syntax, functions, classes, modules, and everyday language patterns.
Variables, types, operators, interpolation, optional chaining, and nullish coalescing.
console.log(value);const name = "Ada";
let count = 0;const msg = `Hello, ${user.name}!`;function greet(name = "friend") {
return `Hello, ${name}`;
}const city = user?.profile?.address?.city;const pageSize = input ?? 25;const { id, email, role = "user" } = account;const [first, second, ...rest] = values;const merged = { ...defaults, ...overrides };function sum(...nums) {
return nums.reduce((acc, n) => acc + n, 0);
}Conditionals, loops, switch, early return, and error handling.
if (score >= 90) {
grade = "A";
} else {
grade = "B";
}const label = isActive ? "Enabled" : "Disabled";switch (status) {
case "draft":
return "gray";
case "published":
return "green";
default:
return "blue";
}for (const item of items) {
console.log(item);
}for (const key in object) {
console.log(key, object[key]);
}items.forEach((item, index) => {
console.log(index, item);
});try {
riskyOperation();
} catch (err) {
console.error(err.message);
} finally {
cleanup();
}throw new Error("Invalid input");Declarations, arrow functions, callbacks, currying, memoization, and this binding.
function add(a, b) {
return a + b;
}const double = (n) => n * 2;(() => {
console.log("Runs immediately");
})();function runTask(task, onDone) {
const result = task();
onDone(result);
}function createCounter() {
let count = 0;
return () => ++count;
}const bound = handler.bind(context);fn.call(ctx, a, b);
fn.apply(ctx, [a, b]);const multiply = (a) => (b) => a * b;function memoize(fn) {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) return cache.get(key);
const value = fn(...args);
cache.set(key, value);
return value;
};
}Use built-in collection types and object helpers.
const map = new Map([["id", 1], ["name", "Ada"]]);const unique = new Set([1, 2, 2, 3]);map.set("email", "ada@example.com");
const email = map.get("email");const hasAdmin = roles.has("admin");Object.keys(user);
Object.values(user);
Object.entries(user);const obj = Object.fromEntries([["a", 1], ["b", 2]]);const arr = [...unique];
// or Array.from(unique)const bytes = new Uint8Array([72, 73]);Constructors, inheritance, static members, private fields, and prototype methods.
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, ${this.name}`;
}
}class Admin extends User {
constructor(name, role) {
super(name);
this.role = role;
}
}class MathUtil {
static clamp(n, min, max) {
return Math.min(Math.max(n, min), max);
}
}class Counter {
#count = 0;
inc() {
return ++this.#count;
}
}class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
get area() {
return this.width * this.height;
}
}User.prototype.isNamed = function (value) {
return this.name === value;
};Import/export syntax, dynamic import, CommonJS interop, and environment access.
export function slugify(text) {
return text.toLowerCase().replaceAll(" ", "-");
}export default class ApiClient {}import { slugify } from "./slugify.js";import ApiClient from "./api-client.js";import * as math from "./math.js";const module = await import("./feature.js");const fs = require("node:fs");const apiBase = process.env.API_BASE_URL;