Lesson Weekend

You can call methods on strings, just like you can with numbers. Let's take a look at a few of these methods - including some we've learned about already. Here's one we've learned already:

> "supercalifragilisticexpialidocious".toUpperCase();
"SUPERCALIFRAGILISTICEXPIALIDOCIOUS"

Here's the other string method we've learned - a method so important that we recommend memorizing it (though you can always look up the documentation if you need to):

> const word = "foo";
undefined
> word.concat("bar");
"foobar"

Methods can be chained like this:

> "foo".concat("bar").toUpperCase();
"FOOBAR"

We actually tried out chaining these exact methods when we discussed the distinction between methods and functions. As we can see, when we chain methods together like this, we first concatenate the string - and then uppercase that string.

By the way, here's a very useful shortcut for the concat() method that we recommend memorizing as well.

> "I love" + " " + "Epicodus";
"I love Epicodus"

As we can see, we can just use the + sign to "add" strings together!

And we can do this with variables, too:

> const sentiment = "I love ";
undefined
> const animal1 = "cats";
undefined
> const animal2 = "dogs";
undefined
> sentiment + animal1;
"I love cats"
> sentiment + animal2;
"I love dogs"

Some string methods take numbers as arguments. Here's an example:

> "caterpillar".charAt(5);
"p"

Did you notice something funny about this example? What's the fifth character of the word "caterpillar"? Well, with the way we normally count, "c" is the first letter, and "r" is the fifth. But JavaScript says that the character at the 5th position is "p". That's because computers start counting at zero. So "c" is the zeroth letter, and "r" is the fourth. This is really important - and it's another thing that trips up beginners. However, it will soon become second nature.

Terminology


  • Chaining methods: Calling a method directly on the return value of another method.

  • Concatenation: Combining two Strings together into one String.

Methods


A few useful string methods:

  • charAt(); - Returns the character at a particular location in a String.
  • toUpperCase(); - Converts a String to uppercase.
  • toLowerCase(); - Converts a String to lowercase.
  • concat(); - Combines two strings.

Examples


You can call methods on strings, or variables assigned to strings:

"supercalifragilisticexpialidocious".toUpperCase();
const word = "foo";
word.concat("bar");

You might wonder how we were able to use concat with a constant, but this method (and most others) doesn't change what's known as the receiver - the thing that a method is being called on. In the example above, word is the receiver. And if we check the value of word, it's still "foo", not "foobar". We would need to assign the return value of the expression above to a new variable for "foobar" to be saved.

Methods can be chained (for strings, numbers, or anything else):

"foo".concat("bar").toUpperCase();

You can combine strings with + instead of concat(). This is still known as concatenation despite not using the concat() method:

"I love" + " " + "Epicodus";

Lesson 13 of 27
Last updated more than 3 months ago.