Passing in NULL as a parameter in ES6 does not use the default parameter when one is provided

前端 未结 3 457
星月不相逢
星月不相逢 2020-12-24 04:53

Is there a known reason why passing in null as a parameter in ES6 does not use the default parameter when one is provided?

function sayHello(nam         


        
3条回答
  •  囚心锁ツ
    2020-12-24 05:53

    That's just how default parameters are defined in the spec. See MDN: (emphasis mine)

    Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

    null is neither no value, nor undefined, so it doesn't trigger the default initialization.

提交回复
热议问题