1function greet(name, greeting, message = greeting + ' ' + name) {
2 return [name, greeting, message]
3}
4
5greet('David', 'Hi') // ["David", "Hi", "Hi David"]
6greet('David', 'Hi', 'Happy Birthday!') // ["David", "Hi", "Happy Birthday!"]
7
1function greeting (name = 'stranger') {
2 console.log(`Hello, ${name}!`)
3}
4
5greeting('Nick') // Output: Hello, Nick!
6greeting() // Output: Hello, stranger!
1function say(message='Hi') {
2 console.log(message);
3}
4say(); // 'Hi'
5say(undefined); // 'Hi'
6say('Hello'); // 'Hello'
7
8
9function date(d = today()) {
10 console.log(d);
11}
12function today() {
13 return (new Date()).toLocaleDateString("en-US");
14}
15date();
16
17
18function add(x = 1, y = x, z = x + y) {
19 return x + y + z;
20}
21console.log(add()); // 4
22