slice()  String  instance method getterS2C Home  « Globals « 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
beginThe starting extraction position of the zero-based index.
  • Negative values start extraction from the end of the index backwards, for the number after negation.
endThe ending extraction position of the zero-based index.
  • The end position pertains to the index to stop before.
  • For negative values the characters to miss from end.
  • When omitted extraction is to end of string.

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);

Press the button below to action the above code:


Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings

go to home page Homepage go to top of page Top