[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/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 7.

String 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"

js
slice(indexStart)
slice(indexStart, indexEnd)

indexStart

.

indexEnd Optional

.

.

slice() .

slice() indexEnd . str.slice(4, 8) ( 4, 5, 6, 7 ).

              indexStart        indexEnd
                                 
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| T | h | e |   | m | i | r | r | o | r |

                  m   i   r   r
                 _______________
                      
                    Result
  • indexStart >= str.length, .
  • indexStart < 0, . , max(indexStart + str.length, 0) .
  • indexStart , 0 .
  • indexEnd indexEnd >= str.length slice() .
  • indexEnd < 0 . , max(indexEnd + str.length, 0) .
  • (, indexEnd indexStart ) indexEnd <= indexStart .

slice()

slice() .

js
const str1 = "The morning is upon us."; // str1  23.
const str2 = str1.slice(1, 8);
const str3 = str1.slice(4, -2);
const str4 = str1.slice(12);
const str5 = str1.slice(30);
console.log(str2); // he morn
console.log(str3); // morning is upon u
console.log(str4); // is upon us.
console.log(str5); // ""

slice()

slice() .

js
const str = "The morning is upon us.";
str.slice(-3); // 'us.'
str.slice(-3, -1); // 'us'
str.slice(0, -1); // 'The morning is upon us'
str.slice(4, -1); // 'morning is upon us'

11 16 .

js
console.log(str.slice(-11, 16)); // "is u"

11 7 .

js
console.log(str.slice(11, -7)); // " is u"

5 1 .

js
console.log(str.slice(-5, -1)); // "n us"

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


Web Proxy Viewer  |  New URL  |  Original Page