JS: String.prototype.substring()

The JavaScript substring() function extract a part of a string. It is very useful in many string related programs.

The syntax for .substring() function is given below.

String.substring(startIndex[, endIndex]);

startIndex is number that represents the position of a character from where you want to begin extraction.

endIndex is the number that represents the position of the last character you want to extract. The position always starts at 0.

Note: that the endIndex is optional. If you do not specify endIndex, the substring() function will extract start from a position and extract rest of the string.

Example Program: Substring()

<!DOCTYPE html>
   <title>JS: substring() Function</title>
   <meta charset="utf-8">
<h1>Extract a Substring</h1>
<h2 id="out">Output Here</h2><br/>
<button onclick="extractString()">Extract</button>
function extractString() {

var Str = "Tiger is our national animal";

var output = Str.substring(1,10);

document.getElementById("out").innerHTML = output;

Output: Substring()

Output - Substring() function
Output – Substring() function
Rate this post