Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

JavaScript Arrays

JavaScript arrays are used to store multiple values in a single variable.

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Arrays</h2>

<p id="demo"></p>

<script>

var cars = ["Saab", "Volvo", "BMW"];

document.getElementById("demo").innerHTML = cars;

</script>

</body>

</html>

<!DOCTYPE html>

<html>
<body>

<h2>JavaScript Arrays</h2>

<p id="demo"></p>

<script>

var cars = new Array("Saab", "Volvo", "BMW");

document.getElementById("demo").innerHTML = cars;

</script>

</body>

</html>

Access the Elements of an Array


You access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

<!DOCTYPE html>
<html>

<body>

<h2>JavaScript Arrays</h2>

<p>JavaScript array elements are accessed using numeric indexes (starting from
0).</p>

<p id="demo"></p>

<script>

var cars = ["Saab", "Volvo", "BMW"];

document.getElementById("demo").innerHTML = cars[0];

</script>

</body>

</html>

Changing an Array Element


This statement changes the value of the first element in cars:

<!DOCTYPE html>

<html>
<body>

<h2>JavaScript Arrays</h2>

<p>JavaScript array elements are accessed using numeric indexes (starting from
0).</p>

<p id="demo"></p>

<script>

var cars = ["Saab", "Volvo", "BMW"];

cars[0] = "Opel";

document.getElementById("demo").innerHTML = cars;

</script>

</body>

</html>

Arrays are Objects


Arrays are a special type of objects. The typeof operator in JavaScript returns
"object" for arrays.

But, JavaScript arrays are best described as arrays.


Arrays use numbers to access its "elements". In this example, person[0] returns
John:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Objects</h2>

<p>JavaScript uses names to access object properties.</p>

<p id="demo"></p>

<script>

var person = {firstName:"John", lastName:"Doe", age:46};

document.getElementById("demo").innerHTML = person["firstName"];

</script>

</body>

</html>

fruits = ["Banana", "Orange", "Apple", "Mango"];


var first = fruits[0];
fruits = ["Banana", "Orange", "Apple", "Mango"];
var last = fruits[fruits.length - 1];

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Arrays</h2>

<p>The best way to loop through an array is using a standard for loop:</p>

<p id="demo"></p>

<script>

var fruits, text, fLen, i;

fruits = ["Banana", "Orange", "Apple", "Mango"];

fLen = fruits.length;

text = "<ul>";

for (i = 0; i < fLen; i++) {

text += "<li>" + fruits[i] + "</li>";

text += "</ul>";
document.getElementById("demo").innerHTML = text;

</script>

</body>

</html>

You can also use the Array.forEach() function:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Arrays</h2>

<p>Array.forEach() calls a function for each array element.</p>

<p>Array.forEach() is not supported in Internet Explorer 8 or earlier.</p>

<p id="demo"></p>

<script>
var fruits, text;

fruits = ["Banana", "Orange", "Apple", "Mango"];

text = "<ul>";

fruits.forEach(myFunction);

text += "</ul>";

document.getElementById("demo").innerHTML = text;

function myFunction(value) {

text += "<li>" + value + "</li>";

</script>

</body>

</html>

Associative Arrays
Many programming languages support arrays with named indexes.

Arrays with named indexes are called associative arrays (or hashes).

JavaScript does not support arrays with named indexes.

In JavaScript, arrays always use numbered indexes.

<!DOCTYPE html>
<html>

<body>

<h2>JavaScript Arrays</h2>

<p id="demo"></p>

<script>

var person = [];

person[0] = "John";

person[1] = "Doe";

person[2] = 46;

document.getElementById("demo").innerHTML =

person[0] + " " + person.length;

</script>

</body>

</html>

<!DOCTYPE html>

<html>
<body>

<h2>JavaScript Arrays</h2>

<p>If you use a named index when accessing an array, JavaScript will redefine the array
to a standard object, and some array methods and properties will produce undefined or
incorrect results.</p>

<p id="demo"></p>

<script>

var person = [];

person["firstName"] = "John";

person["lastName"] = "Doe";

person["age"] = 46;

document.getElementById("demo").innerHTML =

person[0] + " " + person.length;

</script>

</body>

</html>
Input Validation Example
This example examines input. If the value is wrong, an exception (err) is
thrown.

The exception (err) is caught by the catch statement and a custom error
message is displayed:

<!DOCTYPE html>

<html>

<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">

<button type="button" onclick="myFunction()">Test Input</button>

<p id="p01"></p>

<script>
function myFunction() {

var message, x;

message = document.getElementById("p01");

message.innerHTML = "";

x = document.getElementById("demo").value;

try {

if(x == "") throw "empty";

if(isNaN(x)) throw "not a number";

x = Number(x);

if(x < 5) throw "too low";

if(x > 10) throw "too high";

catch(err) {

message.innerHTML = "Input is " + err;

</script>

</body>

</html>

You might also like