JavaScript String substring() method

The JavaScript string substring() method retrieves the part of the given string on the basis of the specified index. Original string will not be modified. It is similar to slice, only difference is that it not support the negative index.

Note: It returns the complete string if we do not pass any parameter in the method.

Syntax:

string.substring(start_index, end_index) 

Parameters:
start_index: It represents the position of the string from where the string retrieve starts.
end_index: It represents the position up to which the string retrieve.

Return:
Sub string.

Example:

<!DOCTYPE html>
<html>
<body>
<script>   
var a ="HELLO WORLD!";  
document.write(a.substring(0,5));  
</script>
</body>
</html>

Output:

HELLO
Please Share