Arrays in Javascript

Arrays in Javascript

Arrays in Javascript

In this Blog Post, We can study “Arrays in Javascript”.

In the JavaScript language, the array is a single variable used for storing similar types of elements.

This is used when you want to access multiple values or elements using a single variable.

Three ways to create Array in JavaScript

  • JavaScript array literal
  • Creating an instance of Array directly (By using the new keyword)
  • Using Array Constructor (By using Keyword)

1)By using JavaScript Array literal

Syntax:

var arrayname = [element1, element2……., elementN];

The elements are inside in [] and elements separated by , (comma).

Ex:

<script>

var emp1=[“harsh”,”rj”,”Ratan”];

for (i=0;i<emp1.length;i++){

document.write(emp1[i] + “<br/>”);

}

</script>

Output:

harsh

rj

Ratan

Arrays in Javascript

The length method can be used to calculate the length of the array.


2) Creating an instance of Array directly (By using the new keyword)

Syntax:

var arrayname=new Array();

In this procedure, the new keyword can be used for creating an instance of an array.

Ex:

<script>

var j;

var emp1 = new Array();

emp1[0] = “harsh”;

emp1[1] = “avi”;

// In this we use three variable emp1 to emp3

emp1[2] = “rj”;

for (j=0;j<emp1.length;j++){

document.write(emp1[j] + “<br>”);

}

</script>

Output:

harsh

avi

rj

Arrays in Javascript


3)JavaScript Array Constructor (By using the new keyword)

In this procedure we need to create an instance of array by passing the argument in constructor, So that’s reason we don’t need to pass the value explicitly.

Ex:

<script>

var emp1=new Array(“harsh”,”raje”,”ganesh”);

for (i=0;i<emp1.length;i++){

document.write(emp1[i] + “<br>”);

}

</script>

Output:

harsh
raje
ganesh


Changing value of array element

Ex:

In this example we can change value of bike[0]

var Bikes = [“FZS”, “BMW”, “FABUSE”];
Bikes[0] = “PULSAR”;
document.getElementById(“demo”).innerHTML = Bikes[0];

Output:

PULSAR


Access All elements of Array

var Biks = [“fzs”, “bmw”, “fabuse”];
document.getElementById(“demo”).innerHTML = Biks;


JavaScript Array Methods


Convert Array into Strings:

In JavaScript, the toString() method can be used to convert the array into a string.

Ex:

var Biks = [“fzs”, “bmw”, “fabuse”];
document.getElementById(“demo”).innerHTML = biks.toStrings();


Popping and Pushing:

The popping means to remove last element from array for this pop() method will be used.

var Biks = [“fzs”, “bmw”, “fabuse”];

Biks.pop(); // it will removes fabuse from array

The pushing means to add element into a array

Push() method can be used for this and this will add element at last

var Biks = [“fzs”, “bmw”, “fabuse”];

Biks.push(“Pulsar”); // adds pulsar into a array


Shifting an element

The shifting an element is equivalent to popping only the difference is that shift() method can remove first element

var Biks = [“fzs”, “bmw”, “fabuse”];

Biks.shift(); // it will remove fzs from array


Deleting Elements

var Biks = [“fzs”, “bmw”, “fabuse”];

delete Biks[0] //first element is undefined


Splicing an Array

The splice() method can be used to add element into array

var Biks = [“fzs”, “bmw”, “fabuse”];

Biks.splice(2, 0, “pulsar”, “platina”);

The first argument define position where the element stored

Second parameter (0) defined how many element should be deleted

And the rest of parameters defines new elements will be added.

Splice() method will be returned array with deleted items.


Merge  Two  Arrays

concat()  method can be used to merge two different array

var myGirls = [“Cecilie”, “Lone”];
var myBoys = [“Emil”, “Tobias”, “Linus”];

//The myChildern is third array and it concat myGirls and myBoys array
var myChildren = myGirls.concat(myBoys);

 

Also Read This:

What is javascript

What is awt in java

Notepad for Python

Harsh

Hey Friends, Welcome to thetechbuddy. This blog is about technical or programming knowledge.

Leave a Reply