TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Array splice() Method

JavaScript Array splice() Method with example, javascript array methods, concat() method, every() method, filter() method, forEach() method, join() method, indexOf() method, lastIndexOf() method, map() method, push() method, slice() method, sort() method, unshift() method, toSource() method etc.

<< Back to JAVASCRIPT

JavaScript Array splice() method

The JavaScript array splice() method is used to add/remove the elements to/from the existing array. It returns the removed elements from an array. The splice() method also modifies the original array.

Syntax

The splice() method is represented by the following syntax:

array.splice(start,delete,element1,element2,?,elementn)

Parameter

start - It represents the index from where the method start to extract the elements.

delete - It is optional. It represents the number of elements to be removed.

element1,element2,...,elementn - It is optional. It represent the elements to be inserted.

Return

A new array containing the removed elements.

JavaScript Array splice() method example

Here, we will understand splice() method through various examples.

Example 1

Let's see an example to add an element to the existing array without removing other elements.

<script>
var arr=["Monday","Tuesday","Thursday","Friday"];
var result=arr.splice(2,0,"Wednesday")
document.writeln(arr);
</script>
Test it Now

Output:

Monday,Tuesday,Wednesday,Thursday,Friday

Example 2

Let's see an example to add an element to the existing array while removing other elements.

<script>
var arr=["Monday","Tuesday","Saturday","Sunday","Thursday","Friday"];
var result=arr.splice(2,2,"Wednesday")
document.writeln("Updated array: "+arr+"<br>");
document.writeln("Removed element: "+result);
</script>
Test it Now

Output:

Updated array: Monday,Tuesday,Wednesday,Thursday,Friday
Removed element: Saturday,Sunday

Example 3

Let's see an example to add two elements to the existing array while removing one element.

<script>
var arr=["Monday","Tuesday","Sunday","Friday"];
var result=arr.splice(2,1,"Wednesday","Thursday");
document.writeln("Updated array: "+arr+"<br>");
document.writeln("Removed element: "+result);
</script>
Test it Now

Output:

Updated array: Monday,Tuesday,Wednesday,Thursday,Friday
Removed element: Sunday

Example 4

Let's see an example to remove the elements from the existing array.

<script>
var arr=["Monday","Tuesday","Saturday","Sunday","Thursday","Friday"];
var result=arr.splice(2);
document.writeln("Updated array: "+arr+"<br>");
document.writeln("Removed element: "+result);
</script>
Test it Now

Output:

Updated array: Monday,Tuesday
Removed element: Saturday,Sunday,Thursday,Friday

Next TopicJavaScript Array




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf