0

I am currently doing this but it isn't working:

var tempArray=new Array();
var number = 15;
tempArray[number]='blabla';

           for (var key in tempArray) {
                        alert(tempArray[key]);
                    }

the output that I get is:

in_array function (element) { var retur = false; for (var values in this) { if (this[values] == element) { retur = true; break; } } return retur; }

What am I doing wrong?

3
  • There are no associative arrays in JavaScript; there are only objects. Commented Nov 18, 2010 at 15:49
  • 1
    @Gumbo... Basically there are... Objects actually act as associative arrays. Commented Nov 18, 2010 at 15:51
  • i ran your code and it worked as expected, couldn't reproduce your output. even with an intentionally bad array index all i got was 'undefined' Commented Nov 19, 2010 at 17:22

1 Answer 1

5

In JavaScript we use Objects.

var obj = {};

obj["15"] = "blabla";
obj.fifteen = "blablah";

for(var i in obj) {
    alert(obj[i]);
}
Sign up to request clarification or add additional context in comments.

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.