Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
So long String.indexOf()! Now we can get a boolean value if a string contains our search text.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Wouldn't it be nice if we could use
plain english, when writing JavaScript?
0:00
Well thankfully we can,
with the new startsWith, endsWith and
0:04
includes methods for a searching strings,
so first let's take a look at a couple of
0:09
different ways we could determine if
a string started with a particular value.
0:13
And if you're following along
you'll need to launch the latest
0:18
workspace for this video.
0:20
I'll open up the file starts-with.js
in the first console.log we have
0:22
a regular expression,
testing this during to search string.
0:27
The next console.log checks to
see if the strings zero index
0:31
matches the string a-really.
0:36
And finally, the third console.log
uses the ES 2015 startsWith method
0:40
to check whether the string
begins with the string a-really.
0:46
And if I run this file in the console,
we see that everything returns true.
0:52
Now, it's not groundbreaking syntax but,
0:57
compared with the regular expression and
the index of methods,
1:00
the startsWith method makes our code
much easier to read and understand.
1:04
Next open up the file ends-with.js.
1:12
Not much is different here.
1:16
The first console.log is
still a regular expression.
1:17
Only this time we're testing that the end
of the string matches the search.
1:21
In our index of example became a little
more complex, instead of looking for
1:28
something at the zero index, we're now
checking the length of the string and
1:32
subtracting the length of the test string,
so, we can find that appropriate index.
1:38
And lastly we have the endsWith
method to check whether the string
1:43
ends with the text hyphenated-string,
which again is much easier to read.
1:49
Now over in the file
include.js we have something
1:54
very similar to the previous examples.
1:59
There's a regular expression search,
a search using indexOf,
2:02
which expects the value
to be greater than -1.
2:07
And then a search using the ES 2015
includes method to check whether
2:11
strToSearch includes the word
long anywhere in the string.
2:16
Now all three of the new methods we've
looked take an optional second parameter.
2:22
For startsWith and
includes the second parameter is
2:26
the index from where the search should
start, and the default index is 0.
2:31
So for example back in my startsWith.js
file if I give the startsWith method,
2:36
a second parameter of let's say 5 and
run the file in the console.
2:43
Notice how the third
console.log returns false,
2:50
because strToSearch does not start with
a-really at the position we're indexing.
2:54
Now if I change the text to lly,
save it and
3:02
run the file again,
we see that it now return is true.
3:06
Now the second parameter for
3:12
endsWith is the maximum length
of the searchable string.
3:14
So, this string here has a length of 31.
3:19
So, if I pass a second
parameter of say 21,
3:22
endsWith will only use the first
21 characters for the search.
3:27
So if I run this file in the console,
the console.log returns false.
3:33
So I'm really excited to start using
these new string search methods and
3:40
I hope you are too.
3:44
Stick around for the next stage,
3:45
where we dig into what I call
the cooler parts of ES2015.
3:46
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up