How to greet people differently by using JavaScript

Hello friends, In this JavaScript tutorial, I will show you how to greet people differently according to the first letter of their name. I hope this is going to be very much useful. Here we are going to use to JavaScript methods.

In the below example we are greeting people using JavaScript.

Let’s try to understand step by step :



  • In the code, I have taken four variables speakWord, names, firstLetter and i.
  • In speakWord, there are two strings one is “Hello” and another one is “Good Bye”.
  • In names, we have taken some strings, here we are taking few names as a variable.
  • In firstLetter, at first, we are changing the first letter to lower case and then we are checking if it is ‘j’ or not.
  • i is an integer. we are using i to set a condition.
  • In the code there are three functions speak(), helloSpeaker and byeSpeaker.
  • speak(), it just prints the Hello or Good Bye and the names according to the condition.
  • In helloSpeaker, after checking the condition that the first letter does not start from ‘J’ or ‘j’ it prints the string which says Hello + name.
  • In byeSpeaker, after checking the condition that the first letter is from ‘J’ or ‘j’ it prints the string which says Good Bye + name.

Guess The Number Game Using JavaScript

Greet People in JavaScript

<!DOCTYPE html>
<html>
<head>
  <title>Title Goes Here</title>
</head>
<body>
  <script type="text/javascript">
      var speakWord = "Hello";
      function speak(name) 
      {
          console.log(speakWord + " " + name);
      }

      var speakWord = "Good Bye";
      function speak(name) 
      {
          console.log(speakWord + " " + name);
       }
      (function () 
      {

           var names = ["Rishabh", "John", "jen", "Jason",
                        "Shubham", "Bahu", "Divyesh", "Prajwal", "Priyesh", "jim"];
           for (var i = 0; i < names.length; i++) 
                {
                     var firstLetter = names[i].charAt(0).toLowerCase();
                     if (firstLetter === 'j')
                           {
                               byeSpeaker(names[i]);
                                function byeSpeaker(names)
                                     {
                                         console.log("Good Bye " + names);
                                     }
                           }
                      else 
                           {
                              helloSpeaker(names[i]);
                              function helloSpeaker(names)
                                     {
                                         console.log("Hello " + names);
                                     }
                           }
      }            }
       )();
 </script>
</body>
</html>

 

To check the output go to the console and check it.

( for chrome -> right click -> inspect element -> console tab )

You may also learn,

One response to “How to greet people differently by using JavaScript”

  1. Prajwal Rao says:

    Good it’s working

Leave a Reply

Your email address will not be published. Required fields are marked *