JS String Methods

Remember that methods are actions we can perform. JS provides a number of string methods.

We call, or use, these methods by appending an instance (object) with a period (the dot operator), the name of the method, and opening and closing parentheses: e.g. 'Hello'.methodName().

Does that syntax look a little familiar? When we use console.log() we’re calling the .log() method on the console object. Let’s see console.log() and some real string methods in action!

console.log('hello'.toUpperCase()); // Prints 'HELLO'
console.log('Hey'.startsWith('H')); // Prints true

Let’s look at each of the lines above:

On the first line, the .toUpperCase() method is called on the string instance 'hello'. The result is logged to the console. This method returns a string in all capital letters: 'HELLO'.

On the second line, the .startsWith() method is called on the string instance 'Hey'. This method also accepts the character 'H' as an input, or argument, between the parentheses. Since the string 'Hey' does start with the letter 'H', the method returns the boolean true.

You can find a list of built-in string methods in the following websites:

  1. JS String Methods - W3schools.

  2. String Object - MDN


You'll only receive email when they publish something new.

More from Hammad
All posts