Skip to content

default

In JavaScript, the default keyword marks the fallback branch of a switch statement and the main export of a module. It’s a reserved word, so it can’t name a variable.

Inside a switch statement, the default: clause runs when no case clause matches. A switch can have at most one default clause, and it may sit anywhere among the cases, though it usually comes last. Without a break or return, execution falls through from default into the clauses below it.

In a module, export default marks the value that an import declaration without braces receives, and the importing module chooses its own name for it. A module can have only one default export, alongside any number of named exports made with export. Default exports were added in ES2015 together with modules.

JavaScript default Keyword Syntax

The default keyword appears in switch statements and in default exports:

Language: JavaScript Syntax
switch (<expression>) {
  [case <value>: <statements>] ...
  default: <statements>
}

export default <expression>;
export default function [<name>](<parameters>) { <body> }
export default class [<name>] { <body> }
export { <name> as default };

The function and class forms can leave out the name.

JavaScript default Keyword Examples

The default clause handles every value that no case matches:

Language: JavaScript
> function statusText(code) {
…   switch (code) {
…     case 200:
…       return "OK";
…     case 404:
…       return "Not Found";
…     default:
…       return "Unknown status";
…   }
… }
undefined

> statusText(404)
'Not Found'
> statusText(500)
'Unknown status'

No case matches 500, so control jumps to the default clause.

Here’s a module with a default export, saved as greet.mjs:

Language: JavaScript Filename: greet.mjs
export default function greet(name) {
  return "Hello, " + name + "!";
}

Another module, main.mjs, imports that function under a name of its choice:

Language: JavaScript Filename: main.mjs
import sayHello from "./greet.mjs";
import * as greetModule from "./greet.mjs";

console.log(sayHello("Ada"));
console.log(Object.keys(greetModule));

Running main.mjs prints the greeting and the namespace’s keys:

Language: Shell
$ node main.mjs
Hello, Ada!
[ 'default' ]

The namespace object shows that the default export is stored under the name default.

JavaScript default Keyword Use Cases

  • Handling values that no case clause matches in a switch statement
  • Throwing an error for an unexpected value at the end of a switch
  • Exporting the main function, class, or value of a module
  • Letting importing modules pick their own name for the main export

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026