Skip to content

输出什么?

javascript
function sayHi(name) {
  return `Hi there, ${name}`
}

console.log(sayHi())

A. Hi there, B. Hi there, undefined C. Hi there, null D. ReferenceError

答案: B

解析:

默认情况下,如果不给函数传参,参数的值将为undefined。 上述情况,我们没有给参数name传值。 name等于undefined,并被打印。 在ES6中,我们可以使用默认参数覆盖此默认的undefined值。 例如: function sayHi(name =“Lydia”){...} 在这种情况下,如果我们没有传递值或者如果我们传递undefinedname总是等于字符串Lydia