3

I am newbie to the JavaScript world. I have a doubt how the return statement works in JavaScript.

What I am trying to do is to have a function pass one arguments param and looking whether the param match the key of the exampleData object. If match found I want to return the value and break the looping of each function also break the function I don't want to execute any other statement under the each function. If no match found the function must return null. But currently the function always return null value.

function exampleFunction(param){
    $.each(exampleData, function (key, value) {
        if(key == param){
        return value;
        }
    });
    return null;
}

Any insight into this would highly be appreciated. Thank you.

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
Jetson John
  • 3,759
  • 8
  • 39
  • 53
  • 1
    Are you sure that key and param match? Also, are you sure that value isn't null? – Douglas Barbin Jul 31 '13 at 17:11
  • 7
    Your `return value;` is in a different function. Just use a `for-in` loop instead. –  Jul 31 '13 at 17:12
  • 2
    You don't need to do all this. `exampleData[param]` does almost the same thing. It is equal to `undefined` when param is not in exampleData – Paul Jul 31 '13 at 17:13
  • 1
    possible duplicate of [Jquery each - Stop loop and return object](http://stackoverflow.com/questions/8224375/jquery-each-stop-loop-and-return-object) – apsillers Jul 31 '13 at 17:14
  • 1
    @Paul is right. There's no need to loop at all, unless you actually need to the type coercion of the `==` operator. –  Jul 31 '13 at 17:14
  • @DouglasBarbin Yes I am sure. – Jetson John Jul 31 '13 at 17:15
  • possible duplicate of [Javascript function fails to return element](http://stackoverflow.com/questions/11569516/javascript-function-fails-to-return-element) – Bergi Jul 31 '13 at 17:19

4 Answers4

8

Your example doesn't seem to need a loop. That and returning from the loop-delegate doesn't do anything for you.

You can test if a key appears in an object using the in operator.

function exampleFunction(param){
  return param in exampleData ? exampleData[param] : null;
}
canon
  • 40,609
  • 10
  • 73
  • 97
4

Given Crazy Train's comment, you need to capture the matching value so you can return it later:

function exampleFunction(param) {
    var match = null;
    $.each(exampleData, function (key, value) {
        if (key == param) {
            match = value;
            return false; // to end the $.each early
        }
    });
    return match;
}
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
0

Try this:

function exampleFunction(param){ 
    return exampleData[param];
  }

EDIT: If you want to return null if the parameter is undefined, then take a look at Paul and Crazy Train's comments:

return (typeof exampleData[param] === 'undefined')? null: exampleData[param]; 

OR

return (param in exampleData) ? exampleData[param] : null;
Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
0

There are a few issues with your approach:

  1. The inner function you defined is returning to $.each, not to the outer function, so you never get that value.
  2. You don't need to iterate over the object in the wild way you are. Objects in Javascript are hash tables, you can simply do the following

:

function exampleFunction(param) {
    return exampleData[param];
}

If exampleData[param] is undefined, it will return undefined, otherwise, it will return the value stored there.

If you absolutely need the undefined case to return null instead of undefined, then it becomes a bit more complicated.

function exampleFunction(param) {
    if(exampleData.hasOwnProperty(param) {
        return exampleData[param];
    } else {
        return null;
    }
}