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

4
  • 3
    typeof returns a string. undefined is... undefined. Commented May 23, 2013 at 12:01
  • 1
    Do not use for in-enumerations on arrays!! Commented May 23, 2013 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. Commented May 23, 2013 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. Commented May 23, 2013 at 12:30

7 Answers 7

5

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

Sign up to request clarification or add additional context in comments.

Comments

4

This:

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

Should be:

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

(no need to use typeof)

5 Comments

Ok, the downvotes mean I'm being dumb but can someone explain why? This works correctly (I've double-checked it in Chrome). Is it simply poor style?
+1 You are right, i think the dumbest ones have down-voted this post
Sorry, was sure you wrote if (typeof input[i+1] === undefined) my mistake. Took the liberty too edit your post so I can undo my mistake.
@ShadowWizard - Thanks. I probably should have made it clearer to start with
I made the same mistake as ShadowWizard. Have removed my downvote
3

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

1 Comment

@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..
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");
    }
}

Comments

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/

Comments

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.

Comments

0

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

Use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.