Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return values in javascript

Tags:

javascript

I have a javascript function:

function myFunction(value1,value2,value3)
{
     //Do stuff and 

     value2=somevalue2 //to return
     value3=somevalue3 //to return
}

function call in Code: ....

myFunction("1",value2,value3);

if(value2 && value3)
{
//Do some stuff
}

in this scenario how to pass value2 and value3 to the called method or how to return values in Java script.

like image 992
user695663 Avatar asked May 04 '11 17:05

user695663


People also ask

How do you return a value from a JavaScript function?

JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return. That value can be a constant value, a variable, or a calculation where the result of the calculation is returned.

Do JavaScript functions have to return a value?

A JavaScript function can have an optional return statement i.e. it's optional to return a value. This statement should be the last statement in a function.

How do you return a value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.


7 Answers

You can return an array, an object literal, or an object of a type you created that encapsulates the returned values.

Then you can pass in the array, object literal, or custom object into a method to disseminate the values.

Object example:

function myFunction(value1,value2,value3)
{
     var returnedObject = {};
     returnedObject["value1"] = value1;
     returnedObject["value2"] = value2;
     return returnedObject;
}

var returnValue = myFunction("1",value2,value3);

if(returnValue.value1  && returnValue.value2)
{
//Do some stuff
}

Array example:

function myFunction(value1,value2,value3)
{
     var returnedArray = [];
     returnedArray.push(value1);
     returnedArray.push(value2);
     return returnedArray;
}

var returnValue = myFunction("1",value2,value3);

if(returnValue[0]  && returnValue[1])
{
//Do some stuff
}

Custom Object:

function myFunction(value1,value2,value3)
{
     var valueHolder = new ValueHolder(value1, value2);
     return valueHolder;
}

var returnValue = myFunction("1",value2,value3);

// hypothetical method that you could build to create an easier to read conditional 
// (might not apply to your situation)
if(returnValue.valid())
{
//Do some stuff
}

I would avoid the array method because you would have to access the values via indices rather than named object properties.

like image 200
McStretch Avatar answered Sep 23 '22 06:09

McStretch


Javascript is duck typed, so you can create a small structure.

function myFunction(value1,value2,value3)
{         
     var myObject = new Object();
     myObject.value2 = somevalue2;
     myObject.value3 = somevalue3;
     return myObject;
}


var value = myFunction("1",value2,value3);

if(value.value2  && value.value3)
{
//Do some stuff
}
like image 21
Chris Kooken Avatar answered Sep 26 '22 06:09

Chris Kooken


function myFunction(value1,value2,value3)
{         
     return {val2: value2, val3: value3};
}
like image 36
rob Avatar answered Sep 26 '22 06:09

rob


It's difficult to tell what you're actually trying to do and if this is what you really need but you might also use a callback:

function myFunction(value1,callback)
{
     //Do stuff and 

     if(typeof callback == 'function'){
        callback(somevalue2,somevalue3);
    }
}


myFunction("1", function(value2, value3){
    if(value2 && value3)
    {
    //Do some stuff
    }
});
like image 36
no.good.at.coding Avatar answered Sep 27 '22 06:09

no.good.at.coding


I would prefer a callback solution: Working fiddle here: http://jsfiddle.net/canCu/

function myFunction(value1,value2,value3, callback) {

    value2 = 'somevalue2'; //to return
    value3 = 'somevalue3'; //to return

    callback( value2, value3 );

}

var value1 = 1;
var value2 = 2;
var value3 = 3;

myFunction(value1,value2,value3, function(value2, value3){
    if (value2 && value3) {
        //Do some stuff
        alert( value2 + '-' + value3 );
    }    
});
like image 31
ezmilhouse Avatar answered Sep 27 '22 06:09

ezmilhouse


The return statement stops the execution of a function and returns a value from that function.

While updating global variables is one way to pass information back to the code that called the function, this is not an ideal way of doing so. A much better alternative is to write the function so that values that are used by the function are passed to it as parameters and the function returns whatever value that it needs to without using or updating any global variables.

By limiting the way in which information is passed to and from functions we can make it easier to reuse the same function from multiple places in our code.

JavaScript provides for passing one value back to the code that called it after everything in the function that needs to run has finished running.

JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return keyword.

like image 35
Joomler Avatar answered Sep 24 '22 06:09

Joomler


Its very simple. Call one function inside another function with parameters.

 function fun1()
  {
    var a=10;
    var b=20;

    fun2(a,b); //calling function fun2() and passing 2 parameters
  }
       
 function fun2(num1,num2)
  {
    var sum;
    sum = num1+num2;
    return sum;
  }
    
fun1(); //trigger function fun1
like image 37
raj peer Avatar answered Sep 24 '22 06:09

raj peer