JavaScript中的if语句中的默认条件不执行。

huangapple go评论73阅读模式
英文:

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 &quot;input is defined&quot; 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) !== &#39;undefined&#39;) {
  // 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 &#39;undefined&#39;.

if (typeof param !== &#39;undefined&#39;) 
// 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 &#39;undefined&#39;.

if (typeof param !== &#39;undefined&#39;) 
// 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 ? &quot;undefined&quot; : &quot;Input is defined&quot;; 

// 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 -->

huangapple
  • 本文由 发表于 2023年2月19日 04:54:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定