"in operator"和"in operator"之间有什么区别?和"includes()"js [英] What is the difference between "in operator" and "includes()" js

查看:48
本文介绍了"in operator"和"in operator"之间有什么区别?和"includes()"js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是, includes 是一个数组原型,但是 in 也可以与数组一起使用,所以两者的主要区别是什么?

I mean, includes is an Array prototype but in can be used with arrays too, so what is the main difference between both?

推荐答案

Array.includes() 检查数组中是否存在值,而

Array.includes() checks for the existence of a value in an array, while the in operator checks for the existence of a key in an object (or index in the case of arrays).

示例:

var arr = [5];

console.log('Is 5 is an item in the array: ', arr.includes(5)); // true
console.log('do we have the key 5: ', 5 in arr); // false
console.log('we have the key 0: ', 0 in arr); // true

这篇关于"in operator"和"in operator"之间有什么区别?和"includes()"js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆