You can call methods on strings, just like you can with numbers:
> "supercalifragilisticexpialidocious".toUpperCase(); "SUPERCALIFRAGILISTICEXPIALIDOCIOUS"
Or even call the method on a variable assigned to a string:
> const word = "foo"; undefined > word.concat("bar"); "foobar"
Methods can be chained like this:
> "foo".concat("bar").toUpperCase(); "FOOBAR"
concat() method (which concatenates, or combines, two strings) returns a string, which then has
toUpperCase() called on it. Then,
toUpperCase() returns the final result.
By the way, here's a nice shortcut for the
> "I love" + " " + "Epicodus"; "I love Epicodus"
Or with variables:
> const sentiment = "I love "; undefined > const animal1 = "cats"; undefined > const animal2 = "dogs"; undefined > sentiment + animal1; "I love cats" > sentiment + animal2; "I love dogs"
Back to arguments. String methods can take numbers as arguments, too:
> "caterpillar".charAt(5); "p"
Chaining methods: Calling a method directly on the return value of another method.
Concatenation: Combining two Strings together into one String.
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.
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
"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):
You can combine strings with
+ instead of
concat(). This is still known as concatenation despite not using the
"I love" + " " + "Epicodus";
Lesson 13 of 23
Last updated June 18, 2020