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

Dani AI

Generated

The original confusion from was largely about syntax and data shape. pointed toward arrays and showed two workable approaches. The key takeaways below clarify the syntax error you hit and give a small, more maintainable pattern to use in modern JavaScript.

You cannot declare an indexed element inside a var declaration; declare the array (or object) first, then assign to indexes or properties. Also avoid using a global identifier like name (it maps to window.name in browsers) — pick a descriptive variable such as people or users.

A clearer modern pattern is an array of objects plus template literals for string interpolation:

const people = [
  { name: 'Tom', age: 12 },
  { name: 'Dick', age: 11 }
];

const message = `My name is ${people[0].name} and my age is ${people[0].age}.`;
console.log(message);

Practical tips: prefer let/const instead of var; use console.log() or DOM text insertion rather than document.write(); use template literals (\``) for readable interpolation; be mindful of type coercion when concatenating with+(numbers become strings if one operand is a string); and useArray.map()or loops to build many messages. If you need a simple two-value record but want clearer code than nested arrays, objects (as shown) are easier to read and less error-prone thanarray[0][0]` style indexing.

Recommended Answers

All 7 Replies

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.