Hi,
I want to insert a array variable in a another variable. I know how to get it done in php but not in JavaScript. Please advice how should I code the syntax for variable statement ?

var name[0][0]="tom";
name[0][1]=12;

statement="My name is".name[0][0]."and my age is".name[0][1]";

I searched the net and found articles on strings but could not find anything which just shows how to do this. Any help will be appreciated.

Cheers,
Vishal

var name=[];
name[0]=["tom",12];

I did more searching and its

var name="wht ever text u want" +othervar ;

Thank you for your time.

That concatenates strings, which is not what you asked.

sorry I am new at this and was not precise enough

The way you are showing your variables looks more like an array than simply declaring variables.

You can do this two ways:
first as heilo was showing - using the array;

var name = [];
name [0] = ("tom",12);
name [1] = ("dick",11);
name [2] = ("harry",10);

function writeName1(){
  document.write("your name is "+name[0][0]+" and you are "+name[0][1]+" years old");
}

The result will be

your name is tom and you are 12 years old.


It pulls the information from the very first array, then pulls the first element from the array, followed by the second element for the age.


for the basic variable that you were referring to later on

var name = "tom";
var age = 12;

function writeName(){
  document.write("your name is "+name+" and you are "+age+" years old.");
}

You will get the same result as the first one.

Hope this helps clarify a bit.

Hi macgurl70,
Thanks, I used ur first option.

Cheers,
Vishal

No problem, let me know how it works for you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.