Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript String slice(): Extracting a Portion of a String

  • 16 Sep, 2025
  • Com 0
JavaScript String slice() Method

JavaScript String slice() is a built-in function that extracts a portion of a string and returns it as a new string object, without modifying the original string.

Slicing a string in JavaScript

const main_string = "Corporation"

console.log(main_string.slice(0, 5));

// Output: Corpo

In this code, we passed the starting index and the ending index to the slice() method. 

The string starts with index 0 and extends up to index 4. Note that the end index is not included; it is just up to 4. Meaning 0 to 4. This is the basic overview of the function.

Syntax

string.slice(start_index, end_index)

Parameters

Argument Description
start_index (required, integer)

It represents a zero-based index at which extraction begins.

If it is negative, it is treated as string.length + beginIndex.

end_index (optional, integer) It represents the zero-based index before which extraction ends (exclusive; the character at endIndex is not included).

Omit end index (extract to end)

If you omit the end index from the slice() function, it will take a start point and extract up to the end of the string. It is ideal for suffix extraction.

Slicing a string with only start index in JS

const main_string = "Corporation"

console.log(main_string.slice(5));

// Output: ration

In this case, the starting point is index number 5 up to the last character’s index, and hence the output is “ration”.

Negative start index (Counting from the end)

If the starting index is negative, JavaScript counts from the end of the string. In this case, the last character has an index of -1, the second-to-last character is -2, and so on.

Therefore, if the starting index is -8, the extraction will begin at the 8th character from the end of the string and continue up to the last character (index -1).

Slicing a string with negative starting index in JavaScript

const main_string = "Corporation"

console.log(main_string.slice(-8));

// Output: poration

In this code, the character “n” has an index of -1, and the character “p” has an index of -8. So, -8 to -1 extracts the portion “poration”.

Negative end index

This is somewhat tricky. Let’s say your starting index is 0 and your ending index is -7. That means, you start extracting from the 0th index up to the ending index -7. For a negative index, you count from the end, meaning -1, -2,..-7 characters. So, 0 to -7.

Slicing a string with negative end index in JS

const main_string = "Corporation"

console.log(main_string.slice(0, -7));

// Output: Corp

In this code, the starting index is 0, corresponding to “C”, and the ending index is -7, corresponding to “o”.

So, it excludes the last character “o”, we will have the extracted string “Corp”.

Both negative indexes

What if both indices are negative? Well, in that case, you need to count both indices from the end.

Let’s say we have a start index of -6 and an end index of -2; it will extract between these two points.

const main_string = "Corporation"

console.log(main_string.slice(-6, -2));

// Output: rati

In this code, the starting index is -6, which means that if you count backwards from index -1, it will be a character “r”, and -2 translates to “o”, but that is excluded, so “i” would be the last character of the extracted string and hence the output “rati”.

Slicing the last character from the string

To slice the last character in JavaScript, use negative indexing. For example, pass the -1 argument as the start index to the slice() method, and it will return the last character.

Removing thelast character from a string in JavaScript

const main_string = "Corporation"

last_character = main_string.slice(-1)

console.log(last_character)

// Output: n

Slicing the last three characters from the string

For slicing the last three characters, pass -3 as a start index to the slice() method.

Removing the last three characters from a string in JavaScript

const main_string = "Corporation"

last_three_chars = main_string.slice(-3)

console.log(last_three_chars)

// Output: ion

The output clearly shows that the last three characters are “ion” from the input string.

Empty string

If the input string is empty, the output string will also be empty, because there is nothing to slice.

const empty_string = ""

sliced_str = empty_string.slice(-3)

console.log(sliced_str)

// Output:

As expected, the output is an empty string.

That’s it!

Post Views: 9
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

JavaScript String trim(): Removing Whiltespace Characters
How to Unzip a File in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend