String.slice() : slice « String « JavaScript Tutorial






Syntax

string.slice(num1, num2)
    string.slice(num)

The slice() method returns the characters between the indexed positions num1 and num2.

The first character is in position 0.

If num2 is a negative number, the string counts from the end of the string to end the slice.

If passing a single index, the method will return all characters until the end of the string.

<html>
    <script language="JavaScript">
    <!--
    var myString = new String("This is a test");

    var mySlice = myString.slice(0,6);

    document.write('The first 7 characters of our string, ' + myString);
    document.write(', are: ' + mySlice);
    document.close();

    -->
    </script>
</html>








6.25.slice
6.25.1.String.slice()