簡體   English   中英

如何檢查參數是否 AND 不是 typeof 字符串?

[英]How can I check if a parameter is AND is not a typeof string?

我正在尋找解決算法,但遇到了一些問題。 在這里,我設法通過使用typeof檢查它是否是一個字符串,但我還需要檢查它是否不是一個字符串,我需要幫助。

function isString(a, b, c) {
  if (typeof a, typeof b, typeof c === 'string') {
    return 'all parameters are strings';
  } else {
    return 'one of the parameters is not a strings';
  }
}

你的 JS 無效。

如果您只想進行一次測試,則可以在將 arguments 傳播到數組后執行一次測試

 const isString = function(a, b, c) { return [...arguments].every(arg => typeof arg === "string")? 'all parameters are strings': 'one of the parameters is not a string' }; console.log(isString("", "", "")) console.log(isString("", 9, ""))

你可以這樣做

function isString(a, b, c) {
  if ((typeof a && typeof b && typeof c) === 'string') {
    return 'all parameters are strings';
  } else {
    return 'one of the parameters is not a strings';
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM