Advertisement
xerocool-101

010 slice method

Apr 20th, 2025 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.49 KB | Software | 0 0
  1. // Syntax
  2. // array.slice(start, end)
  3.  
  4. // Parameter    Description
  5. // start        Index to start slicing (inclusive)
  6. // end          Index to end slicing (exclusive) – optional
  7. // Returns      New sliced array
  8.  
  9. const fruits = ["apple", "banana", "cherry", "date", "fig"];
  10. const sliced = fruits.slice(1, 3);
  11.  
  12. console.log(sliced); // ["banana", "cherry"]
  13. console.log(fruits); // ["apple", "banana", "cherry", "date", "fig"]
  14.  
  15. // Omit end to slice to the end
  16. fruits.slice(2); // ["cherry", "date", "fig"]
  17.  
  18.  
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement