By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

Array Problem

100+
P: 384
I have a variable called num and i want to pass it into the array element number but i get undefined showing on the page, heres what i mean:

If i put 1 into the document.write(array[1]); then it works fine??
Expand|Select|Wrap|Line Numbers
  1. var num = 1;
  2.  
  3. array[1] = 'Test';
  4.  
  5. document.write(array[num]);
Apr 17 '09 #1
Share this Question
Share on Google+
2 Replies


100+
P: 428
define array

Expand|Select|Wrap|Line Numbers
  1. var num = 1, array=[];
  2.  
  3. array[1] = 'Test';
  4.  
  5. alert(array[num]);
Apr 17 '09 #2

dmjpro
100+
P: 2,476
@ziycon
How does your "array[1]" work fine? Neither "array[1]" nor "array[num]" is working.
Array is not a primary data type, so you need to define before you use it.
Apr 20 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.