Languages
[Edit]
EN

JavaScript - String slice() method example

0 points
Created by:
Ela-Davey
663

The slice() method is used to extract a part of a string in JavaScript.


1. Documentation

Edit
Syntax

slice(startIndex)

slice(startIndex, endIndex)

Parameters

startIndex - the index at which to begin extraction. If negative, the string is sliced from string.length + startIndex to the end of the string.

endIndex - the index of the first character to exclude from the returned substring.

ResultA new string containing the sliced part of the string.
Description

The slice() extracts some text from one string and returns it as a new string.

The method extracts up to endIndex (but not including).

2. Practical examples

Edit

Positive indexing

Edit

Negative indexing

Edit

Mixed

Edit

References

Edit
  1. String.prototype.slice() - JavaScript | MDN

Alternative titles

  1. JavaScript - String.prototype.slice() documentation with examples
  2. js - String slice() method documentation with examples
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (documentation)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join