Introduction
When working with JavaScript, developers often get confused between slice(), splice(), and split(). While they sound similar, they serve completely different purposes.
In this article, you'll learn the exact difference, when to use each method, and see working examples you can apply in real projects.
1. slice() – Extract Without Changing Original Array
The slice() method is used to extract a portion of an array without modifying the original array.
Syntax
Example:
Output:
Key Points
- Does NOT modify original array
- Returns a new array
- End index is NOT included
2. splice() – Modify Original Array
The splice() method is used to add, remove, or replace elements in an array.
Syntax
Example
Output
Key Points
- Modifies original array
- Can add/remove/replace elements
- Very powerful but must be used carefully
3. split() – Convert String to Array
The split() method is used on strings, not arrays. It converts a string into an array based on a separator.
Syntax
Example
Output
Key Points
- Works on strings
- Returns an array
- Does NOT modify original string









No comments:
Post a Comment