#
JavaScript Arrays
This tutorial explains to you how the arrays are used in JavaScript.
Here is an example of using arrays in JavaScript:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
var names = new Array("John", "Paul", "Helen");
var names2 = ["John", "Paul", "Helen"];
var names3 = ["John",
"Paul",
"Helen"];
alert(names);
alert(names2);
alert(names3);
alert(names3.length);
alert(names3[1]);
names3.push("Marry");
alert(names3);
names3.pop();
alert(names3);
names3.sort();
alert(names3);
names3.sort().reverse();
alert(names3);
names3 = names3.concat(names3);
alert(names3);
</script>
</body>
</html>
And here is the result of the JavaScript:
You will see the screen above 3 times. And after that :
Conclusion:
- You can create arrays in 3 ways in JavaScript;
- You can add, remove the last element of the array or concatenate an array with another array;
- You can sort ascending/ descending an array in JavaScript.
Info
- In JavaScript, arrays use numbered indexes
- In JavaScript, objects use named indexes
- Arrays are a special kind of objects in JavaScript