slice()
String
instance method
JS Home <<
JS Reference <<
String <<
slice()
Description
Returns a new string comprised of a section of an existing string.
- Any modifications to the new string have no affect on the string it was created from.
- Any modifications to the old string have no affect on the new string created from it.
Syntax
Signature | Description |
---|---|
aString.slice(begin[, end]) | Returns a new string comprised of a section of an existing string. |
Parameters
Parameter | Description |
---|---|
begin | The starting extraction position of the zero-based index.
|
end | The ending extraction position of the zero-based index.
|
Examples
The code below creates a new string composed of a slice of an existing string.
// Create a string variable.
var partOfASong = ['This man went to moe, went to moe a meadow!'];
// Create a new string.
var partOne = partOfASong.slice(0, 21);
alert(partOne);
// Create another new string.
var partTwo = partOfASong.slice(22, -4);
alert(partTwo);
Related Tutorials
JavaScript Basic Tutorials - Lesson 8 - Strings