1 00:00:00,025 --> 00:00:04,630 Wouldn't it be nice if we could use plain english, when writing JavaScript? 2 00:00:04,630 --> 00:00:09,020 Well thankfully we can, with the new startsWith, endsWith and 3 00:00:09,020 --> 00:00:13,620 includes methods for a searching strings, so first let's take a look at a couple of 4 00:00:13,620 --> 00:00:18,000 different ways we could determine if a string started with a particular value. 5 00:00:18,000 --> 00:00:20,560 And if you're following along you'll need to launch the latest 6 00:00:20,560 --> 00:00:22,090 workspace for this video. 7 00:00:22,090 --> 00:00:27,050 I'll open up the file starts-with.js in the first console.log we have 8 00:00:27,050 --> 00:00:31,551 a regular expression, testing this during to search string. 9 00:00:31,551 --> 00:00:36,830 The next console.log checks to see if the strings zero index 10 00:00:36,830 --> 00:00:40,160 matches the string a-really. 11 00:00:40,160 --> 00:00:46,490 And finally, the third console.log uses the ES 2015 startsWith method 12 00:00:46,490 --> 00:00:52,130 to check whether the string begins with the string a-really. 13 00:00:52,130 --> 00:00:57,440 And if I run this file in the console, we see that everything returns true. 14 00:00:57,440 --> 00:01:00,110 Now, it's not groundbreaking syntax but, 15 00:01:00,110 --> 00:01:04,700 compared with the regular expression and the index of methods, 16 00:01:04,700 --> 00:01:10,010 the startsWith method makes our code much easier to read and understand. 17 00:01:12,110 --> 00:01:16,080 Next open up the file ends-with.js. 18 00:01:16,080 --> 00:01:17,318 Not much is different here. 19 00:01:17,318 --> 00:01:21,510 The first console.log is still a regular expression. 20 00:01:21,510 --> 00:01:26,970 Only this time we're testing that the end of the string matches the search. 21 00:01:28,760 --> 00:01:32,810 In our index of example became a little more complex, instead of looking for 22 00:01:32,810 --> 00:01:38,280 something at the zero index, we're now checking the length of the string and 23 00:01:38,280 --> 00:01:43,840 subtracting the length of the test string, so, we can find that appropriate index. 24 00:01:43,840 --> 00:01:49,210 And lastly we have the endsWith method to check whether the string 25 00:01:49,210 --> 00:01:54,940 ends with the text hyphenated-string, which again is much easier to read. 26 00:01:54,940 --> 00:01:59,290 Now over in the file include.js we have something 27 00:01:59,290 --> 00:02:02,160 very similar to the previous examples. 28 00:02:02,160 --> 00:02:07,150 There's a regular expression search, a search using indexOf, 29 00:02:07,150 --> 00:02:11,120 which expects the value to be greater than -1. 30 00:02:11,120 --> 00:02:16,125 And then a search using the ES 2015 includes method to check whether 31 00:02:16,125 --> 00:02:21,390 strToSearch includes the word long anywhere in the string. 32 00:02:22,630 --> 00:02:26,692 Now all three of the new methods we've looked take an optional second parameter. 33 00:02:26,692 --> 00:02:31,510 For startsWith and includes the second parameter is 34 00:02:31,510 --> 00:02:36,330 the index from where the search should start, and the default index is 0. 35 00:02:36,330 --> 00:02:43,016 So for example back in my startsWith.js file if I give the startsWith method, 36 00:02:43,016 --> 00:02:48,710 a second parameter of let's say 5 and run the file in the console. 37 00:02:50,740 --> 00:02:54,805 Notice how the third console.log returns false, 38 00:02:54,805 --> 00:03:02,450 because strToSearch does not start with a-really at the position we're indexing. 39 00:03:02,450 --> 00:03:06,640 Now if I change the text to lly, save it and 40 00:03:06,640 --> 00:03:10,320 run the file again, we see that it now return is true. 41 00:03:12,640 --> 00:03:14,435 Now the second parameter for 42 00:03:14,435 --> 00:03:19,590 endsWith is the maximum length of the searchable string. 43 00:03:19,590 --> 00:03:22,670 So, this string here has a length of 31. 44 00:03:22,670 --> 00:03:27,212 So, if I pass a second parameter of say 21, 45 00:03:27,212 --> 00:03:33,440 endsWith will only use the first 21 characters for the search. 46 00:03:33,440 --> 00:03:40,200 So if I run this file in the console, the console.log returns false. 47 00:03:40,200 --> 00:03:44,060 So I'm really excited to start using these new string search methods and 48 00:03:44,060 --> 00:03:45,310 I hope you are too. 49 00:03:45,310 --> 00:03:46,630 Stick around for the next stage, 50 00:03:46,630 --> 00:03:50,400 where we dig into what I call the cooler parts of ES2015.