you’re on your way 4
179
asynchronous applications
Introducing the JavaScript substring() function
JavaScript has a function that’s just perfect for taking a string like the coffee
maker is returning, and breaking it up into a few parts. Here’s how you use
the substring() function:
var newString = myString.substring(startIndex, endIndex);
myString is the variable that
holds the string you want to
break up into several smaller parts.
newString is the smaller
substring returned by JavaScript.
substring() is the JavaScript
function you can use to break
down strings into smaller parts.
startIndex is the position in
myString you want your substring
to begin at. Remember, “0”
is the position of the rst
character, not “1”.
1Jim
0
1
2
3
Position:
myString
You can use myString.length to get
the length of a string, which will
always be one greater than the
last position in the string.
endIndex is the position in myString
that you want your substring to end
at. The character at this poisition is
not included in the substring.
4
myString.length
It may help you to think of
substring() as creating a string
from position startIndex to
position (endIndex - 1).