Strings are a fundamental part of all programming languages. In JS, a string is any text inside double or single quotes. They store a series of characters like “Trycoder”, “JavaScript”, and so on. Hence string methods assist you in carrying out a variety of string operations.
There are so many string methods that are available in JS such as concat( ) that joins strings together, indexOf( ) that returns the index of a specified text that is found in a given string, replace( ) which replaces a specified value with another value in a string, etc. In this lesson, we shall take a look at a few of them.
Let us consider the case of string concatenation. We will be making use of the concat( ) method on two stings below.
var string1 = “I am a student, and I am ”;
var string2 = “learning javaScript”;var string3 = string1.concat(string2);
Let us go further by looking at another example. In this case, let’s make use of the replace( ) method to replace “student” with “ programmer”. Consider the following code
var string1 = “I am a student, and I am ”;
console.log(string1.replace(“student”, “programmer”));
Another very important and most common string method is the .length that returns the length of the string. Let us try to get the length of our first string.
var string1 = “I am a student, and I am”
console.log(string1.length);
So we have come to the end of this session. I hope it was an exciting and very educative lesson. For more, stay tuned to this platform. We wish you the best as you code.
Comments
Post a Comment
Please do not enter any spam link in the comment box.