ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# indexOf 和 includes # 区别 - indexOf返回的是数值型的而includes返回的是布尔型的 - 都可以支持第二参数,而且的第二个参数都支持负数形式 - 数组中的indexOf不能判断数组中是否有NaN而includes可以 ### 判断稀疏数组结果不同 ``` <pre class="calibre13">``` <span class="token2">var</span> ary <span class="token">=</span> <span class="token1">[</span><span class="token1">,</span><span class="token1">,</span><span class="token1">]</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>ary<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span>undefined<span class="token1">)</span><span class="token1">)</span><span class="token6">//-1</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>ary<span class="token1">.</span><span class="token3">includes</span><span class="token1">(</span>undefined<span class="token1">)</span><span class="token1">)</span><span class="token6">//true</span> ``` ``` 这是因为`indexOf`认为稀疏数组,省略掉的值是不存在的,但`includes`认为是undefined # 字符串的indexOf和数组中的indexOf的比较 1. 这两个方法都可以接收两个参数 2. 这两个方法在没有查找的指定的字符都返回-1 3. 字符串中的indexOf中的第二个参数不支持负数而数组的indexOf支持 4. 字符串的indexOf在传入参数不是字符串的情况下默认会转换为字符串而数组的indexOf不会进行数据类的转换 字符串的参数不支持负数 ``` <pre class="calibre13">``` <span class="token2">let</span> str <span class="token">=</span> <span class="token4">"abcd"</span><span class="token1">;</span> <span class="token2">let</span> ary <span class="token">=</span> <span class="token1">[</span><span class="token4">"a"</span><span class="token1">,</span><span class="token4">"b"</span><span class="token1">,</span><span class="token4">"c"</span><span class="token1">,</span><span class="token4">"d"</span><span class="token1">]</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>str<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">"a"</span><span class="token1">,</span> <span class="token">-</span><span class="token5">1</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//0</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>ary<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">"a"</span><span class="token1">,</span> <span class="token">-</span><span class="token5">1</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//-1</span> <span class="token5">1</span> <span class="token5">2</span> <span class="token5">3</span> <span class="token5">4</span> ``` ``` 字符串的参数会自动转换 ``` <pre class="calibre13">``` <span class="token2">let</span> str <span class="token">=</span> <span class="token4">"1"</span><span class="token1">;</span> <span class="token2">let</span> ary <span class="token">=</span> <span class="token1">[</span><span class="token4">"1"</span><span class="token1">]</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>str<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token5">1</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//0</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>ary<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token5">1</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//-1</span> ``` ``` ### 字符串的slice和数组中的slice的比较 1. 在不传入任何参数的情况下都是把原来的值拷贝一份 2. 字符串的slice的第二个参数是不支持负数的而数组的可以 3. 都可以接收两个参数 ### 字符串的concat和数组中的concat的比较 1. 这两个方法都没有限定参数,而且参数可以是任何数据类型的值