JavaScript String slice() Method | javascript substring slice
ExampleExtractpartsofastring:letstr="Helloworld!";str.slice(0,5) //Returns"Hello"TryitYourself»[1]More"TryitYourself"examplesbelow.DefinitionandUsageTheslice()methodextractspartsofastringandreturnstheextractedpartsinanewstring.Usethestartandendparameterstospecifythepartofthestringyouwanttoextract.Thefirstcharacterhastheposition0,thesecondhasposition1,andsoon.Tip:Useanegativenumbertoselectfromtheendofthestring.BrowserSupportslice()isfullysupportedinallbrowsers:ChromeIEEdgeFirefoxSafariOpera...
ExampleExtract parts of a string:
let str = "Hello world!";str.slice(0, 5) // Returns "Hello"
Try it Yourself »[1]More "Try it Yourself" examples below.
Definition and UsageThe slice() method extracts parts of a string and returns the extracted parts in a new string.
Use the start and end parameters to specify the part of the string you want to extract.
The first character has the position 0, the second has position 1, and so on.
Tip: Use a negative number to select from the end of the string.
Browser Supportslice() is fully supported in all browsers:
Chrome IE Edge Firefox Safari Opera Yes Yes Yes Yes Yes Yes Syntax Parameter Values Parameter Description start Required. The position where to begin the extraction. First character is at position 0 end Optional. The position (up to, but not including) where ...