In JavaScript, you can specify default values for function parameters. This is particularly useful for optional parameters where you want to ensure a specific default behavior if the caller does not provide certain arguments. Default parameter values can be set during the function declaration.
function getGreeting(email, name = "there") {
console.log(`Hello ${name}, welcome! You've registered your email: ${email}`);
}
getGreeting("[email protected]", "Lane");
// Hello Lane, welcome! You've registered your email: [email protected]
getGreeting("[email protected]");
// Hello there, welcome! You've registered your email: [email protected]
If the second parameter is omitted, the default value "there" will be used in its place. Optional parameters (those with default values) should be defined after all mandatory parameters to avoid ambiguity.
Textio allows you to save contacts, so you can easily send them messages. Complete the createContact function. It should take three parameters:
phoneNumbername, with a default parameter of 'Anonymous'avatar, with a default parameter of 'default.jpg'If a phoneNumber is not passed, it should return the string "Invalid phone number". Otherwise, create a new string by concatenating the given avatar to the string "/public/pictures/", for example: /public/pictures/boots.jpg.
Then return a string in this exact format: Contact saved! Name: NAME, Phone number: PHONE_NUMBER, Avatar: AVATAR_FILEPATH. Replacing NAME, PHONE_NUMBER, and AVATAR_FILEPATH with the given contact info.