How to call methods

Hi , can anyone tell me why I am getting output as

  1. cleaning this table
  2. undefined
    in console when I use console.log(myroom.cleaningtble());

and

  1. cleaning this table .
    when i use myroom.cleaningtble(). for the following code
var myroom = {
    table: "this  table ",
    computer: "this computer ",
    cleaningtble() {
        console.log(`cleaning ${this.table}`)
    }
};

//case 1
console.log(myroom.cleaningtble());
//case 2
myroom.cleaningtble();

why am I getting undefined in the first case .

The cleaningtble function has no return statements, so it returns undefined by default. The other console.log call picks that up and prints it.

1 Like

i re wrote the code but still getting the same output.

var myroom = {
    table: "this  table ",
    computer: "this computer ",
    cleaningtble() {
        return console.log(`cleaning ${this.table}`);
    }
};

//case1 
console.log(myroom.cleaningtble());
//case 2
myroom.cleaningtble();

can you explain it a bit more .

The console.log function returns undefined. So the cleaningtble function returns the undefined returned by that console.log.

If you want case 1 to just be "cleaning ...", just return `cleaning ${this.table}` inside cleaningtble. Case 2 would then have no visible output, but that’s normal.

OK i did what u said

The console.log function returns undefined . So the cleaningtble function returns the undefined returned by that console.log .

It worked.
what i don’t get is why it worked ? can you explain it a bit more in depth .

and why would case 2 wont have a visible output ?

Can you clarify what worked? It’s a bit ambiguous.

If you modified the code like in my previous post, case 2 wouldn’t have a visible output because you’re just calling that function, but not doing anything with it. For example it’s the same with simply calling Math.random();. It does output a random number, but you’re not doing anything with it.

This is what I wrote after reading your suggestion

var myroom = {
    table: "this  table ",
    computer: "this computer ",
    cleaningtble() {
        return `cleaning ${this.table}`;
    }
};

//case1 
console.log(myroom.cleaningtble());

myroom.cleaningtble();

I got the out put as cleaning this table.

now my question is why did i get cleaning this table and undefined as output when I wrote

var myroom = {
    table: "this  table ",
    computer: "this computer ",
    cleaningtble() {
        return console.log(`cleaning ${this.table}`);
    }
};

//case1 
console.log(myroom.cleaningtble());

and only cleaning this table when I write the code like this

var myroom = {
    table: "this  table ",
    computer: "this computer ",
    cleaningtble() {
        return `cleaning ${this.table}`;
    }
};

//case1 
console.log(myroom.cleaningtble());

Because you have two console.log calls. The first one prints the "cleaning ..." string. The second one prints the return value of cleaningtble, which is undefined.

1 Like

Ok ok I got it. Thanks for the help. :slight_smile: