Function with Default Parameters in JavaScript

Level 3137 keystrokes

Default parameters provide fallback values when arguments are not passed.

JavaScript code

// Define a function with default parameter values
// name defaults to "Guest", greeting defaults to "Hello"
1function greet(name = "Guest", greeting = "Hello") {
// Template literal combines greeting and name
2 return `${greeting}, ${name}!`;
3}
// Call without arguments - uses both defaults
4console.log(greet());
// Call with one argument - name is "Alice", greeting uses default
5console.log(greet("Alice"));

Output

Hello, Guest!
Hello, Alice!
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • "× 6
  • (× 5
  • )× 5
  • ;× 3
  • {× 3
  • }× 3
  • ,× 2
  • .× 2
  • `× 2
  • =× 2

More JavaScript examples at level 3

← JavaScript typing practice