String substring() Method - Javascript String

Javascript examples for String:substring

Description

The substring() method extracts the characters from a string and returns the new sub string.

The substring() method does not change the original string.

Parameter Values

Parameter Description
start Required. The position where to start the extraction. First character is at index 0
end Optional. The position up to, not including where to end the extraction. If omitted, it extracts the rest of the string

Return Value:

A new String containing the extracted characters

The following code shows how to Extract characters from 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() {/*from  w ww .j  a  va2  s. c om*/
    var str = "Hello world!";
    var res = str.substring(11, 12);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials