6 Example(s) of JavaScript Array


Description :

In this example, We will see how to declare arrays in javaScript. var vehicles= ["Alto", "Swift", "BMW"]; See the code snippet:


JavaScript Array Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<p id="sample"></p>
<script>
var vehicles= ["Alto", "Swift", "BMW"];
document.getElementById("sample").innerHTML = vehicles;
</script>

</body>
</html>

Output

Description :

In this example, We will see how we can access javaScript array values by index. vehicles[0] will return "Alto". See the code snippet:


JavaScript Array Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<p id="sample"></p>
<script>
var vehicles= ["Alto", "Mahindra", "Audi"];
document.getElementById("sample").innerHTML = vehicles[0];
</script>
</body>
</html>

Output

Description :

In this example, how we declare arrays using new array keyword. var vehicles= new Array("Xuv", "Sonata", "BMW"); See the code snippet:


JavaScript Array Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<p id="sample"></p>
<script>
var vehicles= new Array("Xuv", "Sonata", "BMW");
document.getElementById("sample").innerHTML = vehicles[0];
</script>

</body>
</html>

Output

Description :

In this example, how we get length of array in javaScript? var vehicles= new Array("Maruti", "Zen", "santro"); See the code snippet:


JavaScript Array Example - 4
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<p id="sample"></p>

<script>
var vehicles= new Array("Maruti", "Zen", "santro");
document.getElementById("sample").innerHTML = vehicles.length;
</script>

</body>
</html>

Output

Description :

In this example, How to add an element in javaScript array? Declare array: var vehicles= new Array("Saab", "Volvo", "BMW"); Add element to array: vehicles.push("Audi"); See the code snippet:


JavaScript Array Example - 5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
    <button onclick="addVehicle()">Add the array element</button>
<p id="sample"></p>
<script>
var vehicles= new Array("Saab", "Volvo", "BMW");
document.getElementById("sample").innerHTML = vehicles;

function addVehicle()
{
    vehicles.push("Audi");
    document.getElementById("sample").innerHTML=vehicles;
}

</script>

</body>
</html>

Output

Description :

In this example, declare an array person = [] and assign the values in arrays: var person = []; person[0] = "Michael"; person[1] = "Voughan"; person[2] = 50; document.getElementById("sample").innerHTML = person[1] + " " + person.length; See the code snippet:


JavaScript Array Example - 6
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
  <p id="sample"></p>

<script>
var person = [];
person[0] = "Michael";
person[1] = "Voughan";
person[2] = 50; 
document.getElementById("sample").innerHTML =
person[1] + " " + person.length;
</script>
</body>
</html>

Output