JavaScript Arrays

JavaScript array; Through tutorial, i am going to explain you about JavaScript arrays [] with examples.

JavaScript Array

JavaScript array is a variable, which is hold different data type elements in it.

Note that:- An element data type can be numbers, strings, and objects, etc.

Define array in javaScript

See the following way to define or declare array in javaScript; as shown below:

1 – Using Array constructor

Here, i will take an example for declare array using constructor in javaScript; as shown below:

let arr = new Array();

The arr array is empty i.e. it holds no element.

Next, i will take an second example for array with elements; as shown below:

let arr = new Array(9,10,8,7,6);

2 – Using [] notation

To define or declare array using literal [] notation, define arrays.

See the following examples:

let arr = [element1, element2, element3];

The array literal form uses the square brackets [] to wrap a comma-separated list of elements.

See the following example, to creates num array:

let num = [4, 3, 2];

An array with different type elements

According to javaScript array defination, an array can hold different data types of elements in it.

Here, i will take an simple example for demostration; how to declare or define array with different data types; as shown below:

var arr = ["hello", 520, "test", 960, true];  

To get elements in an array

If you want to get or access elements from an array in javaScript; so you can see the following example for that; as shown below:

For example; if you have an array with elements; as shown below:

var arr = ["hello", 520, "world"];  

And you want to get or access the elements of the arrarray in javascript; as shown below for how to access or get elements from an array in javaScript:

console.log(arr[0]); // 'hello'
console.log(arr[1]); // 520
console.log(arr[2]); // 'world'

To Get Length of an array

To find or get the length of an array in javaScript; so you can use the length property of it.

See the following example:

var arr = ["hello", 520, "world"];  
console.log(arr.length); // 3

The javascript array length property is writable. It’s very good benefit to you. For what?, So you can add or remove elements in an array by changing the value of the length property.

For examples,

Change the length property of the arr array to 5 to add one new element with an initial value of undefined at the end of the array.

// add 1 element
var arr = ["hello", 520, "world"];  
arr.length = 5;
console.log(arr[3]); // undefined

Likewise, To remove elements from an array in js, define the length property of the arrarray to 3 to remove the last two elements of the array.

Then. to access the third element of the array, it returns undefined.

// remove the last 3 elements
var arr = ["hello", 520, "world"];  
arr.length = 3;
console.log(arr[3]); // undefined

To check if variable an array in JavaScript

You can use the typeof operator or isArray() function to check if variable is an array or not in JavaScript.

When you can use typeof operator method with an array in javascript, it returns object as shown in the following example:

let arr = ['hello', 'my', 'world'];
console.log(typeof arr); // object

Check if variable is array javascript, you use Array.isArray() method.

See the following example:

console.log(Array.isArray(arr)); // true

Add and Remove Element From Array JavaScript

If you want to add the elements of beginning and end from an array and remove elements of beginning and end from an array, you can use the following methods in js:

  • push(...items) adds items to the end.
  • pop() removes the element from the end and returns it.
  • shift() removes the element from the beginning and returns it.
  • unshift(...items) adds items to the beginning.
  • splice  removes from a specific Array index.

Conclusion

JavaScript Array tutorial; In this tutorial, you have learned how to declare array in javascript and how to add, remove, and get elements from array in javascript.

More JavaScript Tutorials

Leave a Comment