The JavaScript string slice() method retrieves a part of the given string on the basis of the specified index. It is similar to substring, the only difference is that it supports the negative index. In the case of the negative index, it starts retrieving the sub-string from the end of the string.
Syntax:
string.slice(start_index, end_index)
Parameters:
start_index: It represents the position of the string from where the string retrieves starts.
end_index: It represents the position up to which the string is retrieved.
Return:
Sub string.
Example:
<!DOCTYPE html> <html> <body> <script> var str ="Best tutorial website: w3schools"; document.write(str.slice(5, 13)); document.write("</br>"); document.write(str.slice(0)); document.write("</br>"); document.write(str.slice(5)); document.write("</br>"); document.write(str.slice(-5, -1)); </script> </body> </html>