2016-09-23 10 views
0
私は、インデックス値が 0なぜ別の値

なぜ 'abc'.indexOf([])=== 0?

let index = 'any string'.indexOf([]); 
console.log(index); // 0 

の文字列のインデックスを取得しよう

+0

戻ります。空集合と同様に、すべての集合の部分集合です。 –

答えて

2

String.prototype.indexOfは、検索パターンとして与えられたオブジェクトを文字列に変換します。

[]の文字列表現は、''(空文字列)です。あなたが任意の文字列にindexOf('')を適用する場合は
、それはすべての文字列が空の文字列が含まれている0

console.log([].toString()); 
 

 
console.log('qwerty'.indexOf([])); // this one gives the same result 
 
console.log('qwerty'.indexOf('')); // as this one 
 

 
console.log(['a', 1].toString()); 
 

 
console.log('gfdsa,12345'.indexOf(['a', 1])); // these two give 
 
console.log('gfdsa,12345'.indexOf('a,1')); // the same result as well

関連する問題