Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple values in javascript?

Tags:

javascript

Is there a way to use a sort of the C#-like out or ref parameter modifiers with Javascript to do something like this:

function myManyReturnFunction(number1, number2, out x, out y) {
    x = number1 * number2;
    y = number1 / number2;

    return true;
}

var height1, height2 = 0;
var check = myManyReturnFunction(1,1, out height1, out hight2);

I would like to change the variable's reference as well. So yes, passing an argument by reference.

like image 950
dexter Avatar asked Feb 08 '11 19:02

dexter


1 Answers

function myManyReturnFunction(number1, number2) {
    return {
        x: number1 * number2,
        y: number1 / number2
    }
}

And you don't need x and y parameters, simply call:

var result = myManyReturnFunction(6, 9);
var x = result.x;
var y = result.y;
like image 87
Tomasz Nurkiewicz Avatar answered Sep 28 '22 17:09

Tomasz Nurkiewicz