JavaScript Arrays – Problems, tips, tricks and examples

In JavaScript the arrays are used to store multiple values in one/same variable. Each value in array is known as element and each element stores in separate index. The index is the each memory cell were the each element is stored in array. The value of index starts from 0.

Syntax:

var arrayname  = [item1, item2,….];

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Arrays</h2>
<p id="darkweb"></p>
<script>
var fruits = ["Pink", "Orange", "red", "yellow"];
var first = fruits[0];
document.getElementById("darkweb").innerHTML = first;
</script>
</body>
</html>

Looping Array Element

Loop is used to repeats the set of statements in a program.

In this concept (Looping Array Element) loop can be used in array. So,all the variable stored in array can be accessed in sequence for specific purposes.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JsArrays</h2>
<p id="darkweb"></p>
<script>
var cars, text, fLen, i;
fruits = ["BMW", "Audi", "Fiat", "Toyota"];
fLen = fruits.length;
text = "<ul>";
for (i = 0; i <fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
document.getElementById("darkweb").innerHTML = text;
</script>
</body>
</html>

How To Replace Item In Array With Examples

want to change array zero position then use below example to get an idea, how ro replace item in array.

var cars, text, fLen, i;
fruits = ["BMW", "Audi", "Fiat", "Toyota"];
fruits[0] = "Ooodi";
document.getElementById("daerweb").innerHTML = fruits[0];

In upcoming tutorials, we will talk about javascript methods, sorting, and functions that will help you a lot to solve your project issue.

Stay in touch. Ask Questions.
Lean on us for help, strategies and expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *