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.

Code snippet to say hello in JavaScript with sample console output.
function sayHello(name, { style = "standard" } = {}) {
  switch(style) {
    case "standard": {
      console.log(`👋 Hello ${name}!`);

    case "casual": {
      console.log(`hey ${name.toLowerCase()}`);

    case "shout": {
      console.log(`📣 HELLO ${name.toUpperCase()}, MY FRIEND, GOOD TO SEE YOU.`);

    case "texas": {
      console.log(`🤠 Howdy ${name}!`);

    default: {
      throw new Error(`Unknown greeting style: ${style}`);
>> sayHello("Marie");
👋 Hello Marie!
>> sayHello("Marie", { style: "texas" })
🤠 Howdy Marie!
>> sayHello("Marie", { style: "casual" })
hey marie
>> sayHello("Marie", { style: "shout" })
>> sayHello("Marie", { style: "ANGRY" })
Error: Unknown greeting style: ANGRY

Testing out the function

Let's try running our function as a node script in terminal and see what happens!

Video of Marie running the function in her terminal.


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?

Audio of Marie describing the sayHello options.
Audio with captions (using a video element) of Marie describing the sayHello options.


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.