GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript String slice() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript String slice() Method

Last Updated : 1 Jun, 2026

The slice() method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string.

Syntax:

string.slice(startingIndex, endingIndex);

Parameters:

This method uses two parameters. This method does not change the original string.

  • startingIndex: It is the start position and it is required(The first character is 0).
  • endingIndex: (Optional)It is the end position (up to, but not including). The default is string length.

Return Values:

It returns a part or a slice of the given input string.

Example 1: Slicing String

The code slices the string "Geeks for Geeks" into three parts using the slice() method based on specified indices and logs each part separately.

JavaScript
let A = 'Geeks for Geeks';
b = A.slice(0, 5);
c = A.slice(6, 9);
d = A.slice(10);

console.log(b);
console.log(c);
console.log(d); 

Example 2: Negative start or end index case

The code slices the string "Ram is going to school" into multiple parts using the slice() method with various index combinations and logs each part separately.

JavaScript
let A = 'Ram is going to school';

// Calling of slice() Method
b = A.slice(0, 5);

// Here starting index is 1 given
// and ending index is not given to it so
// it takes to the end of the string  
c = A.slice(1);

// Here endingindex is -1 i.e, second last character
// of the given string.
d = A.slice(3, -1);
e = A.slice(6);
console.log(b);
console.log(c);
console.log(d);
console.log(e); 
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.