英文:
Javascript default condition in if statement not executing
问题
我正在尝试编写一个 if 语句,如果输入已定义,则打印“input is defined”,否则打印 undefined。
function isItUndefined(param)
{
console.log(typeof(param))
if (typeof(param) !== undefined)
{
return 'Input is defined'
}
return undefined
}
console.log(isItUndefined(5))
console.log(isItUndefined(null))
console.log(isItUndefined(undefined))
上面的代码然而给出了以下输出,因为即使条件为假时,默认语句也不会执行:
number
Input is defined
object
Input is defined
undefined
Input is defined
<details>
<summary>英文:</summary>
I am trying to write an if statement that prints "input is defined" if the input is defined, otherwise, undefined is printed.
function isItUndefined(param)
{
console.log(typeof(param))
if (typeof(param) !== undefined)
{
return 'Input is defined'
}
return undefined
}
console.log(isItUndefined(5))
console.log(isItUndefined(null))
console.log(isItUndefined(undefined))
The above code however gives the following output, as the default statement does not execute even when the condition is false:
number
Input is defined
object
Input is defined
undefined
Input is defined
</details>
# 答案1
**得分**: 2
```javascript
`typeof` 返回一个 `string`,所以你的代码应该是:
```javascript
if (typeof(param) !== 'undefined') {
// 打印
}
MDN 实际上有一个专门的页面介绍如何检测 undefined
值以及你可能在每种方法中遇到的问题。例如,在这里使用 typeof
可能会导致一个相对容易通过单元测试捕捉的 bug:如果你在 param
中写错了一个字母,这个 if 语句几乎总是会为 true
。
<details>
<summary>英文:</summary>
`typeof` returns a `string`, so your code should be:
```javascript
if (typeof(param) !== 'undefined') {
// Print
}
MDN actually has a dedicated page on how to detect undefined
values and what kind of hiccups you might run into with each method. For example, using typeof
here might actually cause a bug that's relatively easy to catch with a unit test: if you made a typo in param
, this if statement is going to be true
almost always.
答案2
得分: 1
The typeof
operator returns a string, so either compare the input directly with the value undefined
, or compare typeof param
with the string 'undefined'
.
if (typeof param !== 'undefined')
// or
if (param !== undefined)
英文:
The typeof
operator returns a string, so either compare the input directly with the value undefined
, or compare typeof param
with the string 'undefined'
.
if (typeof param !== 'undefined')
// or
if (param !== undefined)
答案3
得分: 0
你不需要使用 typeof
就能做到这一点。
function isItUndefined(param) {
return param === undefined ? "undefined" : "输入已定义";
// 如果目标是打印返回值,你可能更喜欢在两种情况下都返回一个字符串,以保持一致的返回类型。
}
console.log(isItUndefined(5))
console.log(isItUndefined(null))
console.log(isItUndefined(undefined))
console.log(isItUndefined())
英文:
You don't need typeof
to do that
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function isItUndefined(param) {
return param === undefined ? "undefined" : "Input is defined";
// If the goal is to print the return value you may prefer to return a string in both cases, in order to have a consistent return type.
}
console.log(isItUndefined(5))
console.log(isItUndefined(null))
console.log(isItUndefined(undefined))
console.log(isItUndefined())
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论