[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/slice [Back]  [Original]

String.prototype.slice() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.slice()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

slice() .

In this article

const str = "The quick brown fox jumps over the lazy dog.";

console.log(str.slice(31));
// Expected output: "the lazy dog."

console.log(str.slice(4, 19));
// Expected output: "quick brown fox"

console.log(str.slice(-4));
// Expected output: "dog."

console.log(str.slice(-9, -5));
// Expected output: "lazy"

str.slice(beginIndex[, endIndex])

beginIndex

, ( ). , str.length + beginIndex (, beginIndex -3, str.length - 3). beginIndex Number(beginIndex), 0.

beginIndex str.length, .

endIndex

, ( ). .

*endIndex * undefined str.length, slice() . , str.length + endIndex (, endIndex -3, str.length - 3). undefined Number(endIndex), .

endIndex startIndex, (, slice(-1, -3) slice(3, 1) "").

, .

slice() . .

slice() endIndex, . str.slice(1, 4) ( 1, 2 3).

, str.slice(2, -1) .

: slice()

slice() .

js
let str1 = " .";
let str2 = str1.slice(1, 8);
let str3 = str1.slice(4, -2);
let str4 = str1.slice(12);
let str5 = str1.slice(30);

console.log(str2); // : 
console.log(str3); // :  
console.log(str4); // :  .
console.log(str5); // : ""

: slice()

slice() .

js
let str = " .";
str.slice(-3); //  '.'
str.slice(-3, -1); //  ''
str.slice(0, -1); //  ' '

11- , - 16- .

js
str.slice(-11, 16); //  ' '

6- , - 7- .

js
str.slice(6, -7); //  ''

: 5- , 1- .

js
str.slice(-5, -1); //  ''

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.slice


Web Proxy Viewer  |  New URL  |  Original Page