Javascript function always returns null

前端 未结 4 455
遥遥无期
遥遥无期 2021-01-20 02:04

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-20 02:49

    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;
    

提交回复
热议问题