0

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..

CobaltBabyBear
  • 2,188
  • 6
  • 26
  • 47
  • 3
    `typeof` returns a string. `undefined` is... undefined. – Shadow The GPT Wizard May 23 '13 at 12:01
  • 1
    [Do not use `for in`-enumerations on arrays!!](http://stackoverflow.com/q/500504/1048572) – Bergi May 23 '13 at 12:11
  • Yet another example of why people shouldn't use `typeof foo === "undefined"` to check for the `undefined` value. It causes far more bugs than it "solves". Just keep it simple and test for `input[i+1] == undefined`, and ignore the FUD people spread. They've usually not really thought things through. –  May 23 '13 at 12:23
  • 2
    ...anyway, your test for the last index can fail since a defined index can have the `undefined` value. I really don't know why you're using `for-in` like this, but to test for the last index, you should be comparing the `i+1` to `input.length`. –  May 23 '13 at 12:30

7 Answers7

6

if(typeof input[i+1] === 'undefined') { ... }

Seer
  • 739
  • 4
  • 22
5

This:

if(typeof input[i+1] == undefined){

Should be:

if(input[i+1] === undefined){

(no need to use typeof)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Rob Johnstone
  • 1,704
  • 9
  • 14
3

Undefined should be a string, "undefined", working fiddle: http://jsfiddle.net/asifrc/vRTsE/1/

asifrc
  • 5,781
  • 2
  • 18
  • 22
  • @asim-ishaq It works fine for me in Chrome.. from a console, from the fiddle I linked above, and even a quick page on my local server.. Does the fiddle not work in Chrome for you? I'd be curious to know why.. – asifrc May 23 '13 at 12:15
1

That is because the typeof operator returns an string. You need to compare with a string "undefined" like so:

var input = [
    'one',
    'two'
];

for(var i in input){
    if(typeof input[i+1] == "undefined"){
        console.log("i is last");
    }
}
Software Guy
  • 3,190
  • 4
  • 21
  • 21
0

typeof returns a string. You're comparing it to the undefined value.

Use

if(typeof input[i+1] === "undefined"){
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
0

javascript typeof operator always returns string, so you should compare against 'undefined' like this:

if(typeof input[i+1] === 'undefined')

Here is an updated fiddle - http://jsfiddle.net/Pharaon/V7EJZ/

Sergey Rybalkin
  • 3,004
  • 22
  • 26
0

Turns out that the type of i is string, so to make the code work properly you need to cast it to integer:

for(var i in input){
    if(typeof input[parseInt(i, 10) + 1] === "undefined") {
        console.log(i + " is the last index");
    }
}

Updated fiddle.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208