Returning alert() from a object/constructor function

Hi
Im at the fullstack engineer course finishing the objects unit at the mdn resource
Im practicing on Object oriented programming at the link

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS

under the constructor exersize
get the following code

function Person(name) {
  this.name = name;
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
}

let person1 = new Person('Bob');
let person2 = new Person('Sarah');

i’ve tried both the above and these alternatives

function Person(name) {
  this.name = name;
  this.greeting = function() {
   return  alert('Hi! I\'m ' + this.name + '.');
  };
}
function Person(name) {
  this.name = name;
  this.greeting = function() {
   let msg = alert('Hi! I\'m ' + this.name + '.');
   return msg;
  };
}
function Person(name) {
  this.name = name;
  this.greeting = function() {
   let msg='Hi! I\'m ' + this.name + '.';
   return alert(msg);
  };
}

yet no matter what I do the debug console returns “undefined” after the alert pops up
check debug console bottom left corner

how does one work with object constructors functions and alert method without getting “undefined” attached?

Thanks

The alert object returns undefined, as with console.log. They are output functions, they don’t return the message - rather they output the message and return undefined.

What you could do, is make an alternate alert function that will return the message:

function alertAndReturn(message){
   alert(message);
   return message;
}
return alertAndReturn(Hi! I\'m ' + this.name + '.');

However, this isn’t practical as you can just do

let msg = 'Hi! I\'m ' + this.name + '.';
alert(msg);
return msg;

Blockquote
What you could do, is make an alternate alert function that will return the message:

function alertAndReturn(message){
   alert(message);
   return message;
}
alertAndReturn(;Hi! I\'m ' + this.name + '.';);

Blockquote

Isn’t it same thing i tried at 3?

Blockquote

that one still returned me a undefined at the debug console which is what I’m trying to avoid what are the differences?

As for the practicality I understand the point is having that function as part of the object
why would they give examples that output errors?

No, because you were returning the alert. Read closely, and see the two differences between my code and yours:

//Your code:
let msg='Hi! I\'m ' + this.name + '.';
return alert(msg);

//My code:
let msg = 'Hi! I\'m ' + this.name + '.';
alert(msg);
return msg;

//The main difference here is that I'm returning the message, while you are returning the "alert" function.

alert returns undefined. It does NOT return the value in which it outputs to the user. Therefore, you return the return value of the alert function, which is undefined.

I return the message, which contains the string after alerting it. You simply alert the message and return the value of that alert, which is undefined because, again - window.alert (you use it as simply alert here) returns undefined.

Hmm I see, so returning an alert as far as the console goes is similar to returning a blank console.log();?

No, it is equivalent to returning undefined (or not returning at all, since functions that don’t return anything return undefined), since alert never returns anything, meaning that it will always be undefined.

alert makes a little modal that is meant to show something to the users. It returns undefined, since it outputs it in a different way than simply returning the message it is using (that would be useless anyways, since you can always return the message).

Some functions don’t return anything, so returning them returns their undefined return value.

It’s basically like doing return undefined, since alert("a message") returns undefined.

Similarly, console.log logs the value to the console, but returns undefined. Return values are what the function returns.

Some functions output things, but that doesn’t relate to the return value.

nice! sure clears things up!
thanks :slight_smile:

1 Like

You are very welcome! If this is the answer then can you please mark it as so?

It helps other people with the same question find answered questions faster and also helps people understand that this question is already answered so as to help other people with unanswered problems.

didnt know,check
will do same at previous questions ive posted:)

1 Like