The difference between in and includes in javascript

in

Iterate over objects
let names = {
    
    
   name: 'Alice',
    age:20,
    address:"beijing"
    };
    //name是属性
for(let name in names){
    
    
    console.log("属性是:"+name+"值是:"+names[name])
}
//判断某个属性是否在对象中,属性名字一定要加引号
console.log("name" in names)
console.log("Name" in names)
console.log("age" in names)

Insert picture description here

Iterate over the array
let arr = [7,5]
    for (let i in arr)
    {
    
    
        console.log("索引:"+i+"值"+arr[i])
    }
    console.log(7 in arr)
    console.log("7" in arr)
    console.log(1 in arr)

Insert picture description here
To summarize: in the object name is the attribute in the corresponding object, and the attribute value in the array is the index. To determine whether an attribute should be enclosed in quotation marks in the object, otherwise an undefine error will occur;In the array, whether or not quotation marks are used, the default is an index, not a value! ! ! ! ! Remember

includes usage

The includes() method is used to judge whether an array contains a specified value, if it returns true, otherwise false, the first parameter is the value to be judged, the second is to judge from the index of the array, the default is 0

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  //false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
console.log([1,2,3].includes(1,1))//false

If there are any mistakes, you are welcome to criticize and correct.

Guess you like

Origin blog.csdn.net/qq_44606064/article/details/109545569