Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does this return undefined - javascript

Tags:

javascript

i'm pretty new to js. i'm sorry if this sounds dumb. but why does the following code return "undefined"

function NewPerson(name, age, sex){
    this.name = name;
    this.age = age;
    this.sex = sex;

    this.getName = function(){
        //alert(this.name);
        alert("The age is "+this.age);
    };

}

var obj1 = new NewPerson("Mark",25,"Male");
alert("The age is as follows "+obj1.getName());

// output:

The age is 25 The age is as follows undefined

like image 352
Bharath Avatar asked Feb 03 '26 03:02

Bharath


1 Answers

Because you don't return anything.

like image 198
Dave Newton Avatar answered Feb 04 '26 17:02

Dave Newton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!