The JavaScript string slice() method retrieves a part of the given string on the basis of the specified index. It is similar to substring, only difference is that it support the negative index. In case of 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 retrieve starts.
end_index: It represents the position up to which the string retrieve.
Return:
Sub string.
Example:
<!DOCTYPE html> <html> <body> <script> var str ="Best tutorial website: w3spoint"; 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> |
Output:
tutorial Best tutorial website: w3spoint tutorial website: w3spoint sjav |