for in和for of的简单区别
//for in可以遍历数组和对象,但是for of只能遍历数组,不可以遍历对象 var arr = [1,4,5,6,7,8]; var obj = { name:'za', age:19, say:function(){ console.log('hello') } } //for in循环出的是key, for(var i in arr){ console.log('索引值',i); //索引值 0 1 2 3 4 5 } for(var i in obj){ console.log('属性名',i); //属性值 name age say } //for of循环出的是value for (var item of arr){ console.log('数组项',item); //数组项 1 4 5 6 7 8 } for(var item of obj){ console.log(item); //报错obj is not iterable,对象不可用for of }
控制台打印结果: