Cob*_*ear 0 javascript for-loop
我有以下简单的Javascript代码.
var input = [
'one',
'two'
];
for(var i in input){
if(typeof input[i+1] == undefined){
console.log("i is the last index");
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么,但这个console.log()部分从未执行过.这意味着它永远不会进入if条件,而显然超出最后一个索引的索引是未定义的.
你可以在这个小提琴中看到它.
请解释..
这个:
if(typeof input[i+1] == undefined){
Run Code Online (Sandbox Code Playgroud)
应该:
if(input[i+1] === undefined){
Run Code Online (Sandbox Code Playgroud)
(无需使用typeof)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |