Let's Say Hello
Let's write some JavaScript together! We want a function that takes a name and greets that person.
But we're going to get a bit fancy and show off different styles of greetings we can pass, with a default option set for us.
function sayHello(name, { style = "standard" } = {}) {
switch(style) {
case "standard": {
console.log(`👋 Hello ${name}!`);
break;
}
case "casual": {
console.log(`hey ${name.toLowerCase()}`);
break;
}
case "shout": {
console.log(`📣 HELLO ${name.toUpperCase()}, MY FRIEND, GOOD TO SEE YOU.`);
break;
}
case "texas": {
console.log(`🤠 Howdy ${name}!`);
break;
}
default: {
throw new Error(`Unknown greeting style: ${style}`);
}
}
}
Testing out the function
Let's try running our function as a node script in terminal and see what happens!
Transcript
So over here we have a lovely function called "sayHello" and then we're just going to do some stuff with it.
So let's try saying hello to me.
So if we come over here after saving our file and run our "cool-file.js", we see "Hello". Yay!
Now let's see what happens if we switch up the style of our hello to something like maybe "texas". Run it again... "howdy". [laughs] That's great.
sayHello
options
What are all the different options of saying hello that you can use?
sayHello
options.sayHello
options.Transcript
Here are the different styles that our sayHello function currently supports:
standard
, which is the default, which includes a little waving emoji;casual
, which is all lowercase;shout
, which is all uppercase;texas
, which says "howdy" with a cowboy emoji;
and that's it. Otherwise we throw an error.
But we can always edit our function and add more if we want to.