List of JavaScript array methods and properties

Posted by

1. constructor

The constructor returns the function that created the Array object’s prototype.

Example:

<script>
const countries = [“Pakistan”, “Canada”, “USA”, “China”];
var text = countries.constructor;
// text variable will returns like: function Array() { [native code] }
</script>

2. concat()

The concat() method is used to join (concatenates) two or more arrays and return an array with the joined arrays.

Example:

<script>

const arr1 = [“Jhon”, “Lee”];

const arr2 = [“Emma”, “Tessa”, “Anna”];

const arr3 = arr1.concat(arr2);

// arr3 variable will returns e.g: Jhon,Lee,Emma,Tessa,Anna

</script>

 

3. valueOf()

The valueOf() method returns the primitive value of an array.

Example:

<script>

const countries = [“Pakistan”, “Canada”, “USA”, “China”];

var myArray = countries.valueOf();

// myArray variable will returns like:Pakistan,Canada,USA,China

</script>

 

4. unshift()

The unshift() method used for add new items beginning of an array.

Example:

<script>

const countries = [“Pakistan”, “Canada”, “USA”, “China”];

countries.unshift(“UK”,“Dubai”);

// Output will be like: UK,Dubai,Pakistan,Canada,USA,China

</script>

5. splice()

The splice() method is used to add/remove elements from an array.

Example:

<script>

const countries = [“Pakistan”, “Canada”, “USA”, “China”];

countries.splice(2,0,”United States”, “United Kingdom”);

// countries variable will returns like: Pakistan,Canada,United States,United Kingdom,USA,China

</script>

6. toString()

The toString() converts an array to a string, and the result method returns a string with all array values separated by commas.

Example:

<script>

const countries = [“Pakistan”, “Canada”, “USA”, “China”];

var text = countries.toString();

// text variable will returns like: Pakistan,Canada,USA,China

</script>

Verified by MonsterInsights