Is it better to return `undefined` or `null` from a javascript function?

前端 未结 10 1260
暖寄归人
暖寄归人 2020-12-07 10:58

I have a function which I have written which basically looks like this:

function getNextCard(searchTerms) {
  // Setup Some Variables

  // Do a bunch of log         


        
相关标签:
10条回答
  • 2020-12-07 11:52

    Undefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined.

    From the ECMAScript2015 spec

    4.3.10 undefined value

    primitive value used when a variable has not been assigned a value

    4.3.12 null value

    primitive value that represents the intentional absence of any object value

    http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions-undefined-type

    Further reading:

    When is null or undefined used in JavaScript?

    0 讨论(0)
  • 2020-12-07 11:53

    Depends on what u need to do with the returned value.

    typeof null returns an object. that object has a value of undefined

    typeof undefined returns undefined

    0 讨论(0)
  • 2020-12-07 11:53

    I would argue that in this case, null should be returned.

    If you consider the question from a theoretical computer science point of view then undefined is used to indicate non-termination/ non-computability (i.e. the placeholder for an undefined point x of a partial function f which is often written f(x) = ⊥).

    getNextCard however seems to be able to compute the next card (if it exists) and also be able to compute if there is no next card. In other words, the function is total since it terminates for every input.

    That being said, a special value signalling termination without meaningful result (i.e. "there's no card I can return for this input") is required and this for me is null not undefined.


    NOTES:

    You can see some support for this argument in some other typed languages as well where termination without meaningful result are expressed using an option type (sometimes also referred to as nullable type). An example for this is is Maybe in Haskell.

    On the other hand, we of course do not know what undefined in JavaScript is really supposed to mean. So, the analogy to undefined is a bit tenous. Moreover, since we always want to work with total functions, this amounts to saying "never return undefined from a function". Which seems to be a bit strict, since it would limit the use of undefined to properties/ variables which have not been set.

    In the end, my personal preference is never to return undefined where I can return null and I would also argue that this is the better coding convention (because among other things x !== null is shorter than typeof x !== 'undefined').

    0 讨论(0)
  • 2020-12-07 11:54

    undefined is not something you should assign to. You might want to consider to return something else other than undefined. In your case, even if you don't return anything at all, the result will be undefined already. So, I'd suggest to go with null instead.

    Consider this sample,

    function getSomething() {
         // .. do something
         return undefined;
    }
    
    function doSomething() {
         // .. I'm not gonna return anything.
    }
    
    var a = getSomething();
    var b = doSomething();
    

    Above sample result in a === b, which is undefined. The difference is that you save 1 statement execution.

    0 讨论(0)
提交回复
热议问题