「JavaScript」includesメソッドで配列に特定要素が含まれるかサンプル

2022年1月28日

書式
Array.prototype.includes()
includes() メソッドは、特定の要素が配列に含まれているかどうかを true
または false で返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const cft = [44, 30, 20, 12]
if (cft.includes(20)) {
console.log(cft.includes(20))
console.log('20存在します')
}
if (!cft.includes(50)) {
console.log(cft.includes(50))
console.log('50存在しません')
}
const cft = [44, 30, 20, 12] if (cft.includes(20)) { console.log(cft.includes(20)) console.log('20存在します') } if (!cft.includes(50)) { console.log(cft.includes(50)) console.log('50存在しません') }
const cft = [44, 30, 20, 12]
if (cft.includes(20)) {
  console.log(cft.includes(20))
  console.log('20存在します')
}
if (!cft.includes(50)) {
  console.log(cft.includes(50))
  console.log('50存在しません')
}

実行結果
> true
> “20存在します"
> false
> “50存在しません"

JavaScript

Posted by arkgame