Sumit | Javascript + React
Sumit | Javascript + React

@sumitsaurabh927

8 Tweets 2 reads Mar 02, 2023
Javascript explained:
.slice() method of Arrays
Hey folks, in this thread we'll learn about the slice method in arrays in Javascript.
It is one of the most widely used methods and is among my personal favorites.
Let's begin! 👇
✂️ As the name suggests, slicing is related to cutting and getting a little part of something.
🍕 Think of a slice of pizza or a slice of mango.
🥭 So, slicing mango means cutting a mango and getting a small portion of it.
✂️ Array.slice() method is no different, only instead of mangoes, we're cutting an array.
⬅️ So, when we want to get data out of an array without disturbing the original array, we use Array.slice().
📍 Notice the 'without disturbing' part. it is important for immutability.
🚀 And immutable data operation is a key part of Functional programming but more on that later.
🌟 Let's switch back to our slicing action on arrays & take a look at its fairly simple syntax.
👇 The general syntax is as follows:
🧩 arrayName.slice( indexFrom, indexTo )
🗃️ Here,
✅ arrayName = the name of the array we're calling this function on,
✅ indexFrom = the index in the array from which to start slicing, and
✅ indexTo = the index till which to slice.
✅ Array.slice() then returns a sub-array of corresponding elements.
💡 Note: indexFrom is inclusive & indexTo is exclusive i.e, the element present at 'indexFrom' will be in the returned subarray but the element present at 'indexTo' won't be.
🏅 This is a crucial point, always be considerate of it.
🎯 Interesting questions can be framed here
That's a wrap!
Hey, I write two threads every day on web dev and it takes a lot of time and effort.
So, I'd be glad if you could spare me a minute to:
✅ Follow me @sumitsaurabh927 for more of these.
✅ RT the tweet below to show me some love.

Loading suggestions...