String slice() Method - Javascript String

Javascript examples for String:slice

Description

The slice() method returns the extracted parts in a new string.

Parameter Values

Parameter Description
start Required. position to start the extraction. First character is at position 0
end Optional. position up to, but not including where to end the extraction.

If end is omitted, slice() selects all characters from the start-position to the end of the string

Return Value:

A String, representing the extracted part of the string

The following code shows how to Extract parts of a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*  w w w .  jav  a2s. co  m*/
    var str = "Hello world!";
    var res = str.slice(-1);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials