English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String slice() Method

 JavaScript String Object

slice()extracts a part of the string using the method and returns it as a new string without modifying the original string.

instartandendThe parameter specifies the part of the string to be extracted (endis not included).

The index of the first character is 0, the index of the second character is1and so on.

If omittedendIf the parameter is provided, this method will cut out the rest of the string.

Use negative indices to extract from the end of the string (see the 'More Examples' below).

Syntax:

string.slice(start, end)
var str = 'www.oldtoolbag.com';
var ext = str.slice(3);
Test and See‹/›

Browser Compatibility

All browsers fully support the slice() method:

Method
slice()IsIsIsIsIs

Parameter Value

ParameterDescription
startRequired) The index starting from zero, from which the extraction begins
endOptional) The index starting from zero, at which the extraction terminates. The character at this index is not included. If omittedendIf end is omitted, slice() extracts to the end of the string.

Technical Details

Return Value:A new string containing the extracted part of the string
JavaScript Version:ECMAScript 1

More Examples

The following example uses slice() to extract positions3to9(10-1of the character: :

var str = 'www.oldtoolbag.com';
var ext = str.slice(3, 10);
Test and See‹/›

The following example uses slice() to extract only the last character:

var str = 'www.oldtoolbag.com';
var ext = str.slice(-1);
Test and See‹/›

The following example uses slice() with negative indices:

var str = 'www.oldtoolbag.com';
var ext = str.slice(-8, -3);
Test and See‹/›

 JavaScript String Object