answersLogoWhite

0


Best Answer

The most common and shortest way to write a JavaScript array is:

var myArray=[1,2,3];

You can also use:

var myArray=new Array(1,2,3);

Or:

var myArray=new Array();

myArray[0]=1;

myArray[1]=2;

myArray[2]=3;

All of these produce the same array containing the elements 1, 2, and 3.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

An array in javascript can be created in multiple ways that all creates an instance of the javascript built-in Array object, the most common being this:

var foo = []; //foo now is an array

Other ways are:

var foo = new Array();

or

var foo = Object.create(Array.prototype);

But i dont know of any cases where this would be necessary, and the short way where just assigning empty square braces in a variable declaration is thought of to be best considering it alost always is simpler.

You can also assign indexes to an array at the same time as you create the array.

var foo = [1, 2];

or

var foo = new Array(1, 2);

Note that even though you can use string literals as array indexes, arrays in javascript are thought to only use numerical indexes. For string indexing one should use native objects, that in fact works in a similar way:

var foo = {};

var foo = new Object();

or

var foo = Object.create(Object.prototype);

For both arrays and object indexes (or properties for objects) kan be created or assigned to as this:

foo[1] = null;

foo[2] = 'bar';

foo[3] = 123;

objects:

foo['bar'] = null;

foo['baz'] = 'qux';

You can assign to a array index og object property whatever you can assign to a normal variable.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the correct way to write a JavaScript array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you use a JavaScript variable in the PHP isset function?

You can't actually do that. There is no direct way to make JavaScript code talk to PHP code, as the two languages are interpreted in different locations. The PHP is interpreted by the server, and the JavaScript is interpreted by the client. This means it's easy enough to transfer data from PHP to JavaScript (by generating the JavaScript with the PHP), but not the other way around. If you're simply looking for a way to see if a JavaScript variable is set (from within the JavaScript itself), that can be done with a line like this one: if(myVariable !== undefined){ /* do stuff */} If you actually want to handle it on the PHP side, one way to do so would be to use additional PHP code when that happens. For example: <?php $jsVars = array(); ?> <script type="text/javascript"> var foo = 'bar'; <?php $jsVars['foo'] = 'bar';?> var yub = 'nub'; <?php $jsVars['yub'] = 'nub';?> </script> ... You can then check to see whether a certain variable has been set by seeing if it's in that array: <?php function jsIsset($varname){ global $jsVars; return array_key_exists($varname, $jsVars); } ?> This however, only works when the JavaScript is generated, not when it's interpreted by the client system. For example, imagine you have a variable that is defined by a JavaScript function that is called from an onclick event. By the time that event happens, the page has already been served and the PHP is done executing. If you want the JavaScript to tell the PHP that a variable is defined, you would need to do it through an AJAX request, which I believe is beyond the scope of this question.


How do you write a program which reads a list of ten numbers and print the list in reserve order in c program?

The simplest way is probably to read the numbers into an array and then prints each element of the array starting at the last one and moving backwards.


How do you put java script into HTML?

Javascript inside html <html> <head> <script type="text/javascript" language="javascript"> // Java script code here </script> <body> // html code here </body> </html> Via External Link: <html> <head> <script type="text/javascript" language="javascript" src="location of js file"> </script> <body> // html code here </body> </html>


What is character array?

In other way Character array is called strings.A group of characters can stored in a character array. e.g. char name[] ={'S','A','T','Y','A','\0'};


How do you restart your computer with JavaScript?

There is no known way to restart a computer system in Javascript. Javascript is a web programming language that allows web page functionality such as user input, animation, creatings graphs or charts, and making changes to a page based dynamic data.

Related questions

Can you compute one array in c say and then assign b to a?

Yes, you can. You have to do that in the following way. Each element of A should be assighed each element of B. You have to do it manually means you have to write a loop which will do it for you. In C array A = array B is not correct.


What are good Java script codes?

depending on the intention of the javascript codes you wish to write. there is much you can do with javascript but the only way to find out what "good Java script codes" are is to explore what you want the Javascript to do on the page.


What is the correct way to write hundred and twenty?

The correct way to write it is ' 120 '.The correct way to say it is "One hundred twenty".


What is the correct way to write fireplace?

The correct way to write fireplace is as one word - "fireplace."


Correct way to write 3 am?

the correct way is to simply write 3 and a colon following it then write down 2 zeroes followed by hrs


What is the correct way to write 95 in standard form?

The correct way to write 95 in standard form is 9.5 × 101


What is the correct way to write the year 1012?

1012 is the correct way to write it but if you mean in words the 1 thousand and twelfth


What is the correct way to write New Years' Eve or New Year's Eve?

The correct way is to write New Year's Eve.


How you write spread?

the correct way to write it is like this; spread


What is the correct way to write 'have you had a chance' or 'did you had a chance'?

The correct way to write it is "Have you had a chance?" "Did you had a chance?" is grammatically incorrect and should be "Did you have a chance?"


What is the correct way to write 7200000?

7,200,0000


What is the correct way to write 12midnight?

12 am