Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7630

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T05:38:07+00:00 2024-11-28T05:38:07+00:00

Javascript Array Slice Method

  • 60k

The slice method on arrays returns a shallow copy of a part of an array. It takes two numbers, a start, and an end. Every array has a slice method. Here is a quick example:

let myArray = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let newArray = myArray.slice(2, 3);  console.log(newArray); // [ 'πŸ”‘' ] 
Enter fullscreen mode Exit fullscreen mode

There are two optional parameters for the slice method, start, and end. You can provide both, only start, or neither, if you like – so all of these are valid:

let arrayOne = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayOneSlice = arrayOne.slice(2, 3);  // [ 'πŸ”‘' ]  let arrayTwo = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayTwoSlice = arrayTwo.slice(2);  // [ 'πŸ”‘', 'πŸ”©' ]  let arrayThree = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayThreeSlice = arrayThree.slice();  // [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ] 
Enter fullscreen mode Exit fullscreen mode

start

This is the index of the first item to include in your new array. For example, if set to 2, the slice will start your new array copy at index 2. If a negative is used, it indicates offset from the end of a sequence. For example:

let arrayOne = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayOneSlice = arrayOne.slice(-2);  // [ 'πŸ”‘', 'πŸ”©' ] let arrayOneSliceAgain = arrayOne.slice(-1);  // [ 'πŸ”©' ] 
Enter fullscreen mode Exit fullscreen mode

end

This is the first element to exclude from your sliced array – which is kind of confusing. You might expect end to represent the last item to include – but instead it is the first item to exclude. Keep that in mind if you use slice! If you omit this argument, the slice method will simply continue to the end of the array, as shown in this example:

let arrayTwo = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayTwoSlice = myArray.slice(2);  // [ 'πŸ”‘', 'πŸ”©' ] 
Enter fullscreen mode Exit fullscreen mode

If a value greater than the array length is used, slice only continues to the end of the array. If a negative value is used, it indicates an offset from the end of an array. For example, (2, -1) will go 2 from the start, and -1 from the end of an array:

let arrayOne = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayOneSlice = arrayOne.slice(2, -1);  // [ 'πŸ”‘' ] let arrayOneSliceAgain = arrayOne.slice(1, -1);  // [ 'πŸ”Ž', 'πŸ”‘' ] 
Enter fullscreen mode Exit fullscreen mode

Using slice to make a copy of an array

Slice does not mutate the original array. It instead creates a new shallow copy. As such, your existing array will still continue to contain the same values:

let arrayOne = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayOneSlice = arrayOne.slice(2, 3);    console.log(arrayOne); // [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ] console.log(arrayOneSlice); // [ 'πŸ”‘' ] 
Enter fullscreen mode Exit fullscreen mode

Since slice makes a shallow copy of an array, it is also sometimes used to duplicate and make copies of your array data. For example, an empty slice function will also create a new array in memory – allowing you to have two copies of the same array with the same reference in memory:

let arrayOne = [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]; let arrayOneSlice = arrayOne.slice();  arrayOneSlice[2] = '⚑️' console.log(arrayOne); // [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ] console.log(arrayOneSlice); // [ '⚑️', 'πŸ”Ž', '⚑️', 'πŸ”©' ] 
Enter fullscreen mode Exit fullscreen mode

This can be useful in some scenarios. However, slice only makes a shallow copy of an array. That means things get a little confusing when you have objects in your array. Consider the following array:

let arrayOne = [ { items: [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]}, 'πŸ‘¨β€πŸ’»', 'πŸ˜„', 'πŸ”' ] 
Enter fullscreen mode Exit fullscreen mode

This array contains an object within it. Let's try making a sliced copy of this array, and then update items:

let arrayOne = [ { items: [ '⚑️', 'πŸ”Ž', 'πŸ”‘', 'πŸ”©' ]}, 'πŸ‘¨β€πŸ’»', 'πŸ˜„', 'πŸ”' ] let arrayOneSlice = arrayOne.slice(0, 2);  arrayOneSlice[0].items = [ 'πŸ”Ž' ]; arrayOneSlice[1] = 'πŸ”Ž';  console.log(arrayOne); // [ { items: [ 'πŸ”Ž' ]}, 'πŸ‘¨β€πŸ’»', 'πŸ˜„', 'πŸ”' ] console.log(arrayOneSlice); // [ { items: [ 'πŸ”Ž' ]}, 'πŸ”Ž' ] 
Enter fullscreen mode Exit fullscreen mode

Wait, what? We changed arrayOneSlice's items object, but its changed in both arrayOne and arrayOneSlice! Meanwhile, arrayOneSlice[1] has only changed arrayOneSlice! Welcome to another Javascript quirk. In the first example, using the arrayOneSlice[0].items notation, Javascript interprets this as updating an existing element within the shallow copy and thus it affects the original. However, somewhat confusingly, by using the arrayOneSlice[1] notation, Javascript interprets this as putting a new value into the [1] place of the shallow copy itself.

That means that although it may seem like you are making a copy with slice when working with simple arrays, this does not hold up when using it on more complex objects. Knowing this little piece of trivia is going to save you a lot of time someday.

Conclusion

The Javascript slice method is useful for creating new shallow copies of arrays with a subset of the original arrays data. It can also be used in a limited way to make duplicates that can be independently updated. Javascript slice copies still have the same reference as the original in memory, which is useful to know when manipulating them.

I hope you've enjoyed this guide. For more Javascript, check out my other articles here.

beginnersjavascripttypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.