JavaScript Arrays
JavaScript Arrays
Try it Yourself »
Creating an Array
Using an array literal is the easiest way
to create a JavaScript Array.
Syntax:
Example
Try it Yourself »
Converting an Array to a
String
The JavaScript method toString()
converts an array to a string of (comma
separated) array values.
Example
Result:
Banana,Orange,Apple,Mango
Try it Yourself »
Example
Try it Yourself »
Object:
Try it Yourself »
myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;
cars.length cars.sort()
Example
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
Example
function myFunction(value) {
text += "<li>" + value + "</li>";
}
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
WARNING !
Example
Try it Yourself »
Associative Arrays
Many programming languages support
arrays with named indexes.
Example
Try it Yourself »
WARNING !!
If you use named indexes, JavaScript
will redefine the array to an object.
Example:
Try it Yourself »
Try it Yourself »
A Common Error
Try it Yourself »
Solution 1:
Solution 2:
Try it Yourself »
Example
const myObj = {
name: "John",
age: 30,
cars: [
{name:"Ford", models:["Fiesta",
"Focus", "Mustang"]},
{name:"BMW", models:["320", "X3",
"X5"]},
{name:"Fiat", models:["500",
"Panda"]}
]
}
Example
Try it Yourself »