Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 330109 of String.prototype.slice()

  • Revision slug: JavaScript/Reference/Global_Objects/String/slice
  • Revision title: String.slice
  • Revision id: 330109
  • Created:
  • Creator: jackblack
  • Is current revision? No
  • Comment

Revision Content

Summary

Extracts a section of a string and returns a new string.

Method of String
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 3rd Edition

Syntax

string.slice(beginslice[, endSlice])

Parameters

beginSlice
The zero-based index at which to begin extraction.
endSlice
The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.

Description

slice extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.

slice extracts up to but not including endSlice. string.slice(1,4) extracts the second character through the fourth character (characters indexed 1, 2, and 3).

As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.

Examples

Example: Using slice to create a new string

The following example uses slice to create a new string.

// assumes a print function is defined
var str1 = "The morning is upon us.";
var str2 = str1.slice(4, -2);
print(str2);

This writes:

morning is upon u

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Extracts a section of a string and returns a new string.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>string</em>.slice(<em>beginslice</em>[, <em>endSlice</em>])</code></pre>
<h2 id="Parameters" name="Parameters">Parameters</h2>
<dl>
  <dt>
    <code>beginSlice</code></dt>
  <dd>
    The zero-based index at which to begin extraction.</dd>
</dl>
<dl>
  <dt>
    <code>endSlice</code></dt>
  <dd>
    The zero-based index at which to end extraction. If omitted, <code>slice</code> extracts to the end of the string.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p><code>slice</code> extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.</p>
<p><code>slice</code> extracts up to but not including <code>endSlice</code>. <code>string.slice(1,4)</code> extracts the second character through the fourth character (characters indexed 1, 2, and 3).</p>
<p>As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_slice_to_create_a_new_string" name="Example:_Using_slice_to_create_a_new_string">Example: Using <code>slice</code> to create a new string</h3>
<p>The following example uses <code>slice</code> to create a new string.</p>
<pre class="brush: js">
// assumes a print function is defined
var str1 = "The morning is upon us.";
var str2 = str1.slice(4, -2);
print(str2);
</pre>
<p>This writes:</p>
<pre>
morning is upon u</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/substr" title="JavaScript/Reference/Global_Objects/String/substr">substr</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/substring" title="JavaScript/Reference/Global_Objects/String/substring">substring</a></li>
</ul>
Revert to this revision