We are going to continue with another ESX feature. There are a couple of methods that have been added to the string object wrapper, any sx that allows us to check the beginning or ending of a string. In this movie, we'll take a look at those two methods. Now, both of these methods are supported by modern browsers. But just be aware that they are not supported by iE 11. Now the two methods are pretty easy to remember as they are starts with for checking the start with string or ends with for checking the end of a string.
Now since these are methods of the string object wrapper, we access them using the dot syntax. So here we have an example string that has been created. JavaScript is ubiquitous and let's look at how We would test the start of that string or the end of that string using starts with or ends with. So here we have a sample statement using starts with, we have the variable that contains a string, then we use a dot, and then starts with notice the W is uppercase, and then we put the text that we're looking for. And this would return a true because the first part of this string is Java. And so it would return a true Same thing for this ends with statement.
That end of the string has an S period. And so this would return a true and so you use the dot syntax to access these starts with and ends with. Let's open the console and look at a couple of quick examples. I've entered a simple string this is a JavaScript string that's contained in that string. Let me go ahead and press return that will set up that string within the console. Now let's try starts with and inside of parentheses.
Let's go ahead and enter the string this. And as we can see, that returns true, meaning the string starts with that value that we entered inside of the parentheses. Now what would happen if we entered this inside the parentheses this time? a lowercase t. That of course returns false because it does not match the string. The string starts with an uppercase T, not a lowercase t. Now let's take a look at ends with Inside the parentheses, let's just put one character. And of course that returns true.
Because it does end with that character, we could do a much longer value. And that would return true as well. Now, starts with and ends with has an optional second argument, which we can use and that is a number. And that number actually specifies the position within the string that we should start looking at. So technically, we could use this to compare the text later on in a string, not just at the start or the end of the string. So let me do an example of that was starts with and let's Check for is and what number would we enter in order for this to get a true?
Well, it starts with a 0012345 because strings because it is zero based, we start with zero there. So if I enter f5 and press return, it gives me a true. If I did the state same statement with a different number, of course I get false. Now as with works a bit different than starts with so it doesn't specify what position we begin looking at what it does specify is the length of the string. So right now, the length of str is 28. But let's say we wanted it to be 1234567 the length of seven We could do str dot ends with.
And then we could check to see if it ends with the word is if the length of this string or change to seven, meaning it would cut the rest of this off. No, it doesn't modify the stream. We just use it to specify what we want to check. So let's try that is and then the length of seven. And that returns true. So that's the optional parameter we could use with starts with and ends with.
And these two methods on the string object wrapper are great for checking the beginning and ending of strings. Let's move on to the next topic.