Day 1: Simple Types
Lesson 4: Strings
So far we have looked at constants and variables and how they can store sections of text in between quotations marks and I'm guessing you've just accepted it as a thing "Oh, we can store text".
Well those sections of text are called strings, specifically a string of characters.
These strings might look pretty simple but they actually pack a lot of functionality in, that alone would be enough content for a whole course in itself. Before we get to that let's look at the ways you can make strings.
1const name = "simon"; 2const yourName = "coder 🧑💻"; 3const theirName = `coder 2`;
You can create a string in three ways, you can use double quotes, single quotes and back ticks. So long as you start and end the string with the same type '
or "
or `
then you have a string.
The first two behave the same. Most people have a preference but beyond that it doesn't matter which you use.
Back-ticks come with some extra functionality that we will cover in detail later, the key one you need to know now is that they can contain multi-line strings.
As I said above, strings pack a whole lot of functionality into them, let's see what I mean.
To access that functionality we can use the same technique we use with console.log()
and call what are named methods on your strings. Here's a simple example.
We create a string called name and assign a string with All The Code in it. We then call the method toLowerCase()
on that string which creates a new string that we assign to a constant called lowerCaseName
that we then log out.
Try to change the code above create a new string that is upper case instead of lower case. The name of the new string can be called whatever you want, I've called it newLowerCaseName
but I could have called it myChangedString
- the names of your constants and variables don't have to relate to what they store from a technical perspective, BUT it is good practice to use names that mean something to you when reading your code.
We will come back to strings later on as they can do a lot!