5.12 String

The second built-in object we're going to look at is the string object, so I'm going to define A Message constant and set it to a string. Now we're going to make a Message. and what's going on here? It looks like we have a bunch of properties and methods. But earlier in the course, I told you that the string is a primitive type. Primitive types don't have properties or methods. Only objects do. So why do we see these properties and methods in this string? Well, the reason is that in JavaScript, we have two types of strings. So this is what we call a primitive string but we also have a string object. So we have this constructor object string and we can use it to create a new string object. So we can pass a new string here, now because it's a constructor function, we have to apply the new operator and now we have a string that we're going to call another. Now let's see the type of each of these constants. So here we're going to make a typeof of message, it's a string, the typeof of another is an object. So the first constant is a string primitive, the second is an object. However, when we use dot notation with a string primitive. the JavaScript engine wraps it internally with a string object, we can work with that as a string object. Now, just like the math object, if you want to know more about all of these methods, it's best to consult the documentation to simply look up string in JavaScript. Again, on developer. mozilla. org on this page, you can see all the properties and methods of the string object.In this video, I'm going to show you some of these methods, but I highly recommend that you check out the documentation once, just to take a quick look to see what methods are there in case you need them. So back to our code, let's change this string into this is my first post. Here we have the length property which returns the number of characters in a string. This is particularly useful in situations where we want to make sure that the user types at least a certain number of characters inside an input field. Or maybe you want to set a limit, you don't want the user to type more than 100 characters. Now, if you want to access a character at a given index, you can use square brackets, so, message of 0 returns t, message of 1 returns h. If you want to see that this string has a special word, you can use the includes method. So, does this string contain the word "my" So yes, so logically if I put a random word for example hello, it will return false. We also have another method, startswith this string starts with Here but if you pass a lowercase v here we get false, so note that these searches are case sensitive. We have a similar method, endwith ended with e, so you can see that the last character here is an e. If you want to find the index of a given character or a given string, inside the string, you can use the indexOf method. So let's see what the index of the word my is. So my starts at index 8. We can also replace a part of a string so we do a replace, we want to replace first with second. Note that this returns a new string and does not change the original. So if you save the original, we still have this is my first message. We also have some useful methods like toUpperCase, again this returns a new string where all the characters are capitalized. Similar to this method we have toLowerCase and another useful method is trim. So let me add some white space here, before and after our message. Now, if we call the trim method, it removes all the white spaces before and after our message. And of course, this method has variants, for example we have trimLeft, which only removes the white space at the beginning of the trim string on the right, and so on. Another important concept you need to know about in JavaScript is the escape notation. So if you look at the documentation for the string object, you can see in this table under escape notation, you have these special characters. So if you want to use them, you have to quote them using escape notation. For example, let's say you want to have a single quote in your string. Now, in this example, you have defined this string with only one quote. So, if you want to have a Single quote inside this string, look, our Javascript engine is confused, because it thinks that this second single quote represents the end of the string. To solve this problem, we need to prefix this with a backslash, and now, once this is done, when we save the message, you can see that the quote is actually part of the string. Another very useful method is the split method. So, we make a message. split, with this, we can split a string based on a given character. So here I'm going to skip a blank space, and see what we get, we get an array of 5 items. And each item in this array is a word in our message. That's it for this video on strings in JavaScript, In the next video, we're going to look at literal templates.