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

String.prototype.slice() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String.prototype.slice()

Baseline

20157

slice() String

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

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

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

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

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

js
slice(indexStart)
slice(indexStart, indexEnd)

indexStart

indexEnd

slice() 1

slice() indexEnd indexEnd str.slice(4, 8) 5 8 4567

              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 undefined 0
  • indexEnd undefined 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."; // The length of str1 is 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"

ECMAScript 2027 LanguageSpecification
# sec-string.prototype.slice


Web Proxy Viewer  |  New URL  |  Original Page