Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Full-Stack Software Developer - 16w
Data Science and Machine Learning - 16 wks
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!Start the Challenge
A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.Listen the podcast
In the previous snippet of code, we use
**Array.slice()** to create a sub-array from the array assigned to
animals. In this article we will be exploring how to use the slice method, different solutions, the syntax, parameters that can be used and returned values.
.slice() method creates a shallow copy of some or all of the elements from the original array starting at a specific index and ending at another index. The original array remains unmodified. The
slice function requires two inputs: a starting and an ending index.
The slice method is a useful tool that creates a new array out of the existing one. It contains elements from the start index to the end, though not including this last item. If you don't pass in an end index, it will grab all elements up until the end of the array.
The syntax of the slice() method is as follows:
start (Optional) - The index where the extraction of the elements begins. If
start is omited, is
0 by default. Negative values count back from the end of the array.
end (Optional) - The index where the extraction of the elements ends. The element at this index is not included.
We built an array of strings and named it
courses. By using the slice method, we were able to slice the original array from index 1 up to (but not including) index 3. The
myCourses returned array from the slice method contains the elements 1 and 2. It is important to note that arrays are zero indexed, ****which means the first element of an array is at index
Finding all the items from a selected index in an array till its end is simple. We just omit the second argument and the task is complete:
By using the
slice method with one argument, a brand-new array was created which containes the section from index 3 till the original array's end.
console.log() the original array courses, we can note that the original array remains unchanged.
The slice method also supports negative numbers as parameters to counts back from the end:
In this solution the slice method returned an array with the last two elements from the original array.
Also, we can use two negative parameters to extract a portion of the array counting from the back:
In the previous snippet of code the slice method returned a sub-array from the original array counting from the back.
slice method is a non-destructive way of creating a new array from the original one. It slices off a part of the referenced object without leaving any changes to it.
slice method is incredibly easy to use as it only takes two arguments, streamlining the process for extracting desired elements from an array.
slice method of an array enables us to get any part of the array conveniently. We can indicate a starting and ending index or simply just extract everything till the end. Negative indexing is also allowed, which further widens its potentials. This technique can be incorporated with other array methods to implement various scenarios.
slice method is a great way to create new arrays efficiently and quickly. It helps simplify complex code, making it easier to read and manage, also, you'll save time as you won't have to write as much.