SoFunction
Updated on 2025-04-06

Introduction to the use of substring methods in JavaScript

This method returns a subset of a String object.
grammar

(indexA, [indexB])

The following are the detailed information of the parameters:

  • indexA: An integer between 0 and 1 is less than the length of the string.
  • indexB: (Optional) An integer between the length of 0 and string.

Return value:

  • The substring method returns a new substring based on the given parameter.

example:

<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script type="text/javascript">

var str = "Apples are round, and apples are juicy.";

("(1,2): "  + (1,2));
("<br />(0,10): "  + (0, 10));
("<br />(5): "   + (5));
</script>
</body>
</html>

This will produce the following results:

(1,2): p
(0,10): Apples are
(5): s are round, and apples are juicy.