typestar

ES modules in JavaScript

import and export, with the named and default forms.

export const VERSION = "2.1.0";

export function slugify(title) {
  return title.toLowerCase().replaceAll(" ", "-");
}

export default class Tour {
  constructor(slug, steps) {
    this.slug = slug;
    this.steps = steps;
  }
}

// elsewhere:
// import Tour, { slugify, VERSION } from "./tour.js";
// import * as tour from "./tour.js";
// const { default: Lazy } = await import("./tour.js");

console.log(VERSION, slugify("Language Basics"));

How it works

  1. A module has its own scope; nothing leaks to a global.
  2. export default is one per module, named exports are many.
  3. Imports are hoisted and the specifier must be a literal.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
429
Tokens
70
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 42 seconds.

Type this snippet

Step 1 of 2 in Modules & paths, step 1 of 18 in Node.js.

Next →