Variables in array.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Variables in array.

Post by Goofan »

Hi im abit new with Js i created an easy timer in Js and now i wanna try something new.

Well i tried a few stuffs the ni thought of variables in arrays... i know that u can but normal "words" in an array but how do u do with variables?


var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

Here i use normal words..
Can someone please tell me how i can input variables into the array so that the variable input shows instead of the variables name. =)
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Variables in array.

Post by jraede »

Assuming you have the variables set ahead of time, you can just set the values in the array to the variable. For example,

Code: Select all

 
var mycars = new Array();
var saab = 'saab';
var volvo = 'volvo';
var bmw = 'bmw';
mycars[0] = saab;
mycars[1] = volvo;
mycars[2] = bmw;
 
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Variables in array.

Post by Goofan »

hmm i seem to have something wrong with mine. That didnt work when i tried it. I=ll try it again on another editor and see if i can manage it.
Post Reply