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
There are a few issues with your approach:
return
ing to $.each
, not to the outer function, so you never get that value.:
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;
}
}