var x = “This is a string”;
var pos = x.search(”string”);
Take a look at the above code. It’s all so familiar, yet so new. You can see many of the principles we have discussed previously, and now we are just bringing them all together. You can see the variable.property syntax that we just saw when looking at.length; you can also see the parenthesis and parameter that we saw when we looked at functions. This is because.search() is just a property containing a function that was added to our string when we created it. This function is a pre-defined function that has been written for us, and it is written in a way that allows us to provide a parameter of our search term, and it will then ‘return’ information about where that search term is inside of the string.
So in the above example, the variable ‘pos’ will be assigned the number 10. It will be assigned 10 because the word string can be found at the tenth character into the string. How easy and useful is that? Now, let’s look at another useful string method:
The.slice method is responsible for chopping up our string into a chunk that we specify. This method accepts unto two parameters, a starting position and an ending position. This one is easier to show than to explain, so let’s jump right in with an example: