TheDeveloperBlog.com

Home | Contact Us

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

JavaScript Array slice() Method

JavaScript Array slice() 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 slice() method

The JavaScript array slice() method extracts the part of the given array and returns it. This method doesn't change the original array.

Syntax

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

array.slice(start,end)

Parameter

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

end - It is optional. It represents the index at where the method stops extracting elements.

Return

A new array contains the extracted elements.

JavaScript Array slice() method example

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

Example 1

Let's see a simple example to extract an element from the given array.

<script>
var arr=["AngularJS","Node.js","JQuery","Bootstrap"]
var result=arr.slice(1,2);
document.writeln(result);
</script>
Test it Now

Output:

Node.js

Example 2

Let's see one more example to extract various element from the given array.

<script>
var arr=["AngularJS","Node.js","JQuery","Bootstrap"]
var result=arr.slice(0,3);
document.writeln(result);
</script>
Test it Now

Output:

AngularJS,Node.js,JQuery

Example 3

In this example, we will provide the negative values as index to extract elements.

<script>
var arr=["AngularJS","Node.js","JQuery","Bootstrap"]
var result=arr.slice(-4,-1);
document.writeln(result);
</script>
Test it Now

Output:

AngularJS,Node.js,JQuery

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