Notes

Pro tip: Pass an object as function params and use destructuring

Edit on GitHub

JavaScript
 1interface GreetingParams {
 2  name?: string // keep this for backward compatbility
 3  firstName?: string // replaces name
 4  lastName?: string
 5  lastNameFirst?: boolean
 6}
 7
 8function greet({
 9  name,
10  firstName = name, // if firstName not provided, use name as firstName
11  lastName,
12  lastNameFirst = false,
13}: GreetingParams) {
14  if (!lastName) {
15    return `Hello, ${firstName}`
16  }
17  return `Hello ${lastNameFirst ? lastName + ' ' + firstName : firstName + ' ' + lastName}`
18}
19
20// Backward compatible with old signature
21greet({ name: 'John' })
22// New signature with first name only
23greet({ firstName: 'John' })
24// New signature with first name and last name
25greet({ firstName: 'John', lastName: 'Doe' })
26// New signature with first name and last name, last name first
27greet({ firstName: 'John', lastName: 'Doe', lastNameFirst: true })