7

Difference Between SLICE and SPLICE in JavaScript

 3 years ago
source link: https://dev.to/capscode/difference-between-slice-and-splice-in-javascript-4ahg
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Difference between SLICE & SPLICE in JavaScript

Hello Devs,

In this article, we will discuss what's the difference between the two most important methods of Array in JavaScript (i.e Slice and Splice)

The splice() method

  • returns the removed items in an array.

The slice() method

  • returns the selected element(s) in an array, as a new array object.

The splice() method

  • changes the original array but

slice() method

  • doesn’t change the original array.

Lets see some of the simple examples to get the small clarification.

let arr1=[1,2,3,4,5]
let arr2=[1,2,3,4,5]
arr1.splice(1,3) //returns [2, 3, 4]
console.log(arr1) //[1, 5]

arr2.slice(1,3) //returns [2, 3]
console.log(arr2) //[1, 2, 3, 4, 5]
Enter fullscreen modeExit fullscreen mode

Lets discuss these two methods in more details.

First we will discuss the splice method in detail.

Syntax:

splice(start, deleteCount, item1, item2,... itemN)
Enter fullscreen modeExit fullscreen mode

where,
start (required) -> where you want to start editing the array.

  • if start is not provided then empty array is returned and it will not affect the original array
  • If start is greater than length of array then start will set to length of an array.
  • If negative then it is treated as array.length-start and if now the negative value will come then treat it as 0

deleteCount(optional) -> number of element you want to delete from start index

  • if it is not provided or greater than or equal to array.length then all the element from start to end of an array is deleted.
  • If deleteCount is 0 or negative, no elements will be deleted.

  • item1, item2...itemN(optional) ->The elements to add to the array, beginning from start.

  • If you do not specify any elements, splice() will only remove elements from the array.

lets see some of examples.

let a = [1,2,3,4,5]
a.splice() //[]
console.log(a) // [1,2,3,4,5]

let a = [1,2,3,4,5]
a.splice(2) //returns [3,4,5]
console.log(a) //[1,2]

let a=[1,2,3,4,5]
a.splice(-3) //returns [3,4,5], here start is treated as array.length-start
console.log(a) //[1,2]

let a=[1,2,3,4,5]
a.splice(-7) //returns [1,2,3,4,5], here start is treated as array.length-start and this is ngative so start will now be treated as 0
console.log(a) //[]
//(an empty array)

let a=[1,2,3,4,5]
a.splice(2,9) //returns [3,4,5]
console.log(a) //[1,2]

let a=[1,2,3,4,5]
a.splice(2, 2, 'a','b','c','d') //returns [3, 4]
console.log(a) //[1, 2, "a", "b", "c", "d", 5]
//slice has removed 2 elements starting from index '2' and added the item1, item2, ...itemN at start positon

Enter fullscreen modeExit fullscreen mode

Now lets discuss the slice method.

This method just extract the a part from an array.

Syntax:

slice(start, end)
Enter fullscreen modeExit fullscreen mode

where,
start(required) -> starting index from where to start the extraction from an array.

  • if the value of start is negative then it is treated as arr.length-1
  • if start is undefined, slice starts from the index 0.
  • if start is greater than length of an array, then an empty array is returned.

end (optional)-> denotes till which index (excluding) you want to extract from the start,

  • If end is omitted, then its treated as array.length
  • If end is negative then it is treated as array.length-end
  • If end is non-negative & less than start, then empty array is returned
  • If end is greater than length of an array, then it is treated as array.length

Lets see some of the examples,

let arr=[1,2,3,4,5]
arr.slice() //returns [1,2,3,4,5]
arr.slice(2) //returns [3,4,5]
console.log(arr) //[1,2,3,4,5]

arr.slice(2,1) //returns []
console.log(arr) //[1,2,3,4,5]

arr.slice(2,-1) //returns [3,4], here end is treated as arr.length-1 which is 4 i.e arr.slice(2,4)
console.log(arr) //[1,2,3,4,5]

arr.slice(2,9) //[3,4,5]
console.log(arr) //[1,2,3,4,5]
Enter fullscreen modeExit fullscreen mode

Using slice we can also convert an array like objects to an array ?
Syntax:

Array.prototype.slice.call(arguments)
Enter fullscreen modeExit fullscreen mode

example,

let array_like_obj={
    0: 'john',
    1: 'doe',
    2: 'capscode',
    length: 3
}
console.log(Array.prototype.slice.call(array_like_obj)) 
//["john", "doe", "capscode"]
Enter fullscreen modeExit fullscreen mode

Thank you for reading this far. This is a brief introduction of Difference between SLICE & SPLICE method on Array in JS .
If you find this article useful, like and share this article. Someone could find it useful too. If you find anything technically inaccurate please feel free to comment below.
Now you can also play around the objects in JS.

Hope its a nice and informative read for you.
VISIT https://www.capscode.in/#/blog TO LEARN MORE...

IF MY ARTICLE HELPED YOU

Thanks,
@capscode


Recommend

  • 34

    fastcat - A Faster `cat` Implementation Using Splice Lots of people asked me to write another piece a...

  • 36
    • www.tuicool.com 6 years ago
    • Cache

    JavaScript Splice Tutorial

    JavaScript’s splice is a function that is used to insert, remove and replace items in an array. You use it by choosing a position in the array to work with, choosing how many items to delete, and the new...

  • 16

    Convert between byte array/slice and string yourbasic.org/golang Basics When you convert between a stri...

  • 24

    Convert between rune array/slice and string yourbasic.org/golang Convert string to runes When you...

  • 8
    • abcdxyzk.github.io 4 years ago
    • Cache

    Linux 中的零拷贝技术 splice

    Linux 中的零拷贝技术 splice 2015-05-07 15:26:00 http://hi.baidu.com/renguihuashi/item/ef71f8e28d74f5f22b09a415 ...

  • 6
    • www.z16388.top 3 years ago
    • Cache

    JS使用Splice()函数操作数组

    JS使用Splice()函数操作数组 2019-11-16 9 在js的使用过程中,有一次需要对数组进行各种操作,一时间迫使我想要去使用链表。后来通过查阅资料,总结了下面的一些方法,主...

  • 9

    This site can’t be reached oohcode.com’s server IP address could not be found.

  • 18

    Welcome to LWN.net The following subscription-only content has been made available to you by an LWN subscriber. Thousands of subscribers depend on LWN for the best news from the Linux and free softw...

  • 8

    I recently came across a twitter post where the user challenged others if they knew the difference between the js methods .slice() and .splice() without googling them. A lot of people mixed them up. So I've created this simplified cheat sh...

  • 4
    • lwn.net 2 years ago
    • Cache

    Rethinking splice()

    Rethinking splice() LWN.net needs you!Without subscribers, LWN would simply not exist. Please consider signing...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK