Wednesday, 18 March 2026

Slice vs Splice vs Split in JavaScript – Key Differences with Real Examples

Most Useful HTML Tags

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

  1. Does NOT modify original array
  2. Returns a new array
  3. 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

  1. Modifies original array
  2. Can add/remove/replace elements
  3. 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

  1. Works on strings
  2. Returns an array
  3. Does NOT modify original string

No comments:

Post a Comment

Master CSS Grid: Build Responsive Layouts Easily (With Examples)

Learn 10 powerful CSS3 tricks with real examples to build modern, responsive, and interactive Introduction CSS G...