Master the JavaScript slice() Method with Practical Examples

Anmol singhAnmol singh
3 min read

Understanding the JavaScript slice() Method with Real Examples

If you’ve ever needed just a part of a string in JavaScript, the slice() method is your best friend. It allows you to cut and extract portions of a string and return them as a new string—without modifying the original one.

In this article, we’ll explore how slice() works, including positive and negative indexes, and how it compares with substring().


🧠 What is slice() in JavaScript?

The slice() method returns a portion of a string based on specified start and end indices. It doesn’t change the original string.

jsCopyEditlet str = "ilovecoding";
let part = str.slice(1, 5); 
console.log(part); // => "love"

📌 Syntax

jsCopyEditstr.slice(startIndex, endIndex)

✅ Parameters:

  • startIndex (required): The index at which to begin extraction.

  • endIndex (optional): The index before which to end extraction. The character at this index will NOT be included.


💡 Important Rules

  1. If only one argument is passed (slice(start)), it slices from start to the end of the string.

  2. If a negative value is passed, it counts from the end of the string.

  3. The endIndex is exclusive, not inclusive.


🧪 Examples

✅ Example 1: Slice a Part of the String

jsCopyEditlet msg = "ilovecoding";
console.log(msg.slice(1, 5)); 
// Output: "love"

It starts from index 1 (l) and stops before index 5.


✅ Example 2: Slice Using Only One Index

jsCopyEditlet msg = "ilovecoding";
console.log(msg.slice(5)); 
// Output: "coding"

This slices from index 5 till the end.


✅ Example 3: Using msg.length as End Index

jsCopyEditlet msg = "ilovecoding";
console.log(msg.slice(1, msg.length)); 
// Output: "lovecoding"

msg.length ensures slicing till the last character.


✅ Example 4: Slice with Negative Index

jsCopyEditlet msg = "ilovecoding";
console.log(msg.slice(-1)); 
// Output: "g"

Negative index -1 is converted to msg.length - 1, i.e., the last character.

jsCopyEditlet msg = "ilovecoding";
console.log(msg.slice(-3)); 
// Output: "ing"

-3 becomes msg.length - 3, extracting the last three characters.


🔁 Slice vs Substring

Both slice() and substring() are used to extract string parts, but with key differences:

Featureslice()substring()
Supports negative indexes✅ Yes❌ No
Modifies original string❌ No❌ No
Exclusive end index✅ Yes (end is excluded)✅ Yes (end is excluded)
Syntaxslice(start, end)substring(start, end)

📘 Recap

  • slice(start, end) returns a portion of the string.

  • If end is omitted, it slices till the end.

  • Negative values work from the end of the string.

  • slice() is non-destructive—it doesn't change the original string.


📂 Real-life Use Cases

  • Extracting first name or last name from a full name

  • Getting domain from an email

  • Truncating a string preview for UI/UX design

  • Parsing parts of formatted data


✍️ Final Thoughts

The slice() method is a powerful string manipulation tool in JavaScript. Once you understand how start and end indices work—especially the exclusive nature of the end index and handling negative values—it becomes an intuitive way to handle strings.

0
Subscribe to my newsletter

Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anmol singh
Anmol singh