The function slice () returns part of a string. It takes one or two parameters. If only a single parameter is used, it is interpreted as “from,” so the function returns a substring from that position to the end of the string. This single parameter can be positive (start counting from the beginning of the string) or negative (start counting at the end). Here are some examples:
Код:
myString = "abcdef";
myString.slice (2); //returns "cdef"
myString.slice (-2); //returns "ef"
When slice () is used with two parameters, the first one is interpreted as the start value, and the second is the (noninclusive) stop value. The first one must be positive; the second one can be negative. Again, some examples illustrate:
Код:
myString = "abcdefg";
myString.slice (1, 3); //returns "bc"
myString.slice (1, -2); //returns "bcde"