Sending personalised messages to user using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in two strings. The first string specifies the user name and second the owner name.

If the user and owner are the same our function should return ‘hello master’, otherwise our function should return ‘hello’ appended with the name of that user.

Example

Following is the code −

 Live Demo

const name = 'arnav';
const owner = 'vijay';
function greet (name, owner) {
   if (name === owner){
      return 'Hello master';
   }
   return `Hello ${name}`;
};
console.log(greet(name, owner));

Output

Hello arnav
raja
Published on 17-Apr-2021 16:21:22
Advertisements