Array is the collection of similar types of data value is knows as Array. Array are variable which can hold more than one value. Declaration can be in two ways. Static Declaration and Dynamic Declaration. it can hold many values under a single name.

  • Static Declaration:- It is a size will be fixed during declaration is knows as Static Declaration.
  • Dynamic Declaration:- Array can be declare without size (empty) & size can be determine later declaration is knows as Dynamic Declaration.
Home Page - Basic Engineer
Home Page – Basic Engineer

Syntax

datatype arrayName [];

JAVASCRIPT String Methods

JavaScript Array Methods

There are some important array method in JavaScript. Some of them are as follows:

  • toString()
  • join()
  • pop()
  • push()
  • shift()
  • unshift()
  • delete()
  • concate()
  • sort()
  • Splice()
  • Slice()
  • reverse()

toString()

Convert an array to a string of comma separated values is knows as toString().

Syntax

array.toString() 

Example

<html>
<body>
<script>
 var what=['b','a','s','i','c']; 
 var why=what.toString();
 document.write("hello student "+why);
</script>
</body>
</html>

Output: hello student b,a,s,i,c

join()

joins all the array element using a separator is knows as join().

Syntax

array.join(separator)

Example

<html>
<body>

<script>
var what=["html","java","CSS"]
var why=what.join()
document.write(why);
</script>

</body>
</html>

Output : html,java,CSS

pop()

It is a removes last element from the array is knows as pop().

Syntax

array.pop()  

Example

<html>
<body>

<script>
var what=["HTML","CSS","JAVA"];
document.writeln("hello: "+what+"<br>");
document.writeln("basic: "+what.pop()+"<br>");
document.writeln("student: "+ what);
</script>

</body>
</html>

Output : hello: HTML,CSS,JAVA | basic: JAVA | student: HTML,CSS

push ()

it is a adds a new element at the end of the array is knows as push().

Syntax

array.push(element1,element2....elementn)

Example

<html>
<body>

<script>
var what=["html","CSS"];
arr.push("JAVA");
document.writeln(what);
</script>

</body>
</html>

Output : html ,CSS

shift()

it is a removes first element and returns it is knows as shift().

Syntax

array. shift()  

Example

<html>
<body>

<script>
var what=["HTML","CSS","JAVA"];
var why=what.shift();
document.writeln(why);
</script>

</body>
</html>

Output : HTML

unshift()

it is a adds element to the beginning returns new array length is knows as unshift().

Syntax

array. unshift(element1,element2,....,elementn) 

Example

<script>
var what=["HTML","JAVA"];
var why=what.unshift("CSS");
document.writeln(what); 
</script>

</body>
</html>

Output: CSS,HTML,JAVA

delete()

It is a array element can be delete using the delete operator is knows as delete().

Concat()

it is a use to join array to the given array is knows as Concat().

Syntax

array.concat(. . . . .)  

example

<html>
<body>

<script>
var what=["html","css"];
var why=["javaScipt","java","JSP"];
var result=what.concat(why);
document.writeln(result);
</script>

</body>
</html>

Output: html,css,javaScipt,java,JSP

Sort()

This method is use to sort an array alphabetically is knows as Sort().

Sort() takes an optional compare function. It this function is provides as the first argument the sort() function will consider these values (the values return from the compare function) as the basic of sorting.

Syntax

array.sort(compareFunction)

Example

<html>
<body>

<script>
var what=["JavaScript","JAVA","CSS","HTML"]
var why=what.sort();
document.writeln(why);
</script>

</body>
</html>

Output : CSS, HTML, JAVA, JavaScript

Splice()

Splice can be use to add new items to an array.

Slice()

Slices out a piece from an array it create a new array.

Reverse()

Reverse the element in the source array.

Syntax

array.reverse()

Example

<html>
<body>

<script>
var what=["HTML","CSS","JAVA"];
var why=what.reverse();
document.writeln(why);
</script>

</body>
</html>

Output: JAVA,CSS,HTML

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JavaScripts array please check Wikipedia Click here

Stay Connected Stay Safe. Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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