Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

slice in javascript

slice(beginIndex)
slice(beginIndex, endIndex)

// beginIndex: The index at which to begin extraction
// endIndex: This index will not be included.
// Return: A new string containing the extracted section.

let str1 = 'The morning is upon us.'
// the length of str1 is 23.
let str2 = str1.slice(1, 8) // OUTPUT: he morn
let str3 = str1.slice(4, -2)// OUTPUT: morning is upon u
let str4 = str1.slice(12) // OUTPUT: is upon us.
let str5 = str1.slice(30) // OUTPUT: ""

// NEGATIVES
beginIndex is negative: 
// it is treated as (str.length + beginIndex)
endIndex is negative: 
// it is treated as (str.length + endIndex)
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #slice #javascript
ADD COMMENT
Topic
Name
2+9 =