用途
- オブジェクトに存在するかどうかを調べる
- 配列に対して要素番号が存在するかどうか調べる
使用方法
オブジェクトに存在するかどうかを調べる
var hoge = {
a : "aです"
}
console.log("aです" in obj); //true
console.log("bです" in obj); //false
文言 in 値
というように記述すると、文言
が 値
に存在している場合は true
が、存在しない場合は false
が返ってくる。
配列に対して要素番号が存在するかどうか調べる
var hoge = ["a", "b", "c"];
console.log(0 in hoge); //true
console.log(1 in hoge); //true
console.log(2 in hoge); //true
console.log(3 in hoge); //false
この記事は以下の情報を参考にして執筆しました。