Strings are a JavaScript type to store text values like "bob"
or 'I like cheese'
.
Strings can be created with single or double quotes.
"this is a string"
'this is a string, too'
When a number is surrounded by single or double quotes, it is actually a string!
'3'
"4.32"
Strings can be combined using the plus operator (+). This is called string concatenation.
console.log("bat" + "man"); // prints "batman" to the console
The length
property returns the number of characters in a string.
console.log("coolio".length); // prints 6 to the console
The "coolio" string has 6 letters so the length is 6.
Question | Click to View Answer |
What does the following expression print to the console? console.log("sponge" + "bob");
|
"spongebob"
It is called string concatenation when two strings are combined. |
What does the following expression print to the console? console.log("colombia".length);
|
8
The "colombia" string has 8 letters, so the length is 8. |
What does the following expression print to the console? console.log('i am'.length);
|
4
The 'i am' string has three letters and one space, so the length is 4. Spaces count! |