Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

How does return works with multiple variables in javascript?

Hi I am new to javascript. I am looking at big javascript code already working fine. It contains following statements :

function(a){
  return this.prt = a,this;
}

and in the client code, they are using it as

obj.a(34).a(54)

I want to know what's happening here ? Does javascript allows returning of multiple values.

Pardoning for may be such a silly question. I have googled out, but couldn't find any good references.

Thanks in advance.

Upvotes: 1

Views: 57

Answers (2)

Yasser Shaikh
Yasser Shaikh

Reputation: 47784

I tried running these lines of code on console.

this.name = "yasser";
function demo(){
    return this.name = "neel", 10;
}

And here is what I got,

this.name evaluates to 'neel' and demo() fn returns 10.

So no you cannot return multiple values from javascript. You can return multiple values combining them into one object.

Upvotes: 2

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

I looked at this post How does this return statement with multiple values (variables) works?. However, it's for c language, but I think the rule for comma operator applier here also. Thus, return a,b will result into evaluation of a and then b and returning result of b's evaluation.

Upvotes: 0

Related Questions