I have the following simple Javascript code.
var input = [
'one',
'two'
];
for(var i in input){
if(typeof input[i+1] == undefined){
console.log("i is the last index");
}
}
I don't know if I did something wrong but the console.log() part never executes. Which means it never enters the if condition while clearly the index beyond the last index is undefined.
You can see it in this fiddle.
Please explain..
typeofreturns a string.undefinedis... undefined.for in-enumerations on arrays!!typeof foo === "undefined"to check for theundefinedvalue. It causes far more bugs than it "solves". Just keep it simple and test forinput[i+1] == undefined, and ignore the FUD people spread. They've usually not really thought things through.undefinedvalue. I really don't know why you're usingfor-inlike this, but to test for the last index, you should be comparing thei+1toinput.length.