In this blog series tutorial, I will be covering some of the basic JavaScript programming concepts.
This is geared toward beginners and anyone looking to refresh their knowledge.
See the Previous Level Here
Level 4 will cover:
- Declare String Variables
- Escaping Literal Quotes in Strings
- Quoting Strings with Single Quotes
- Escape Sequences in Strings
- Concatenating Strings with Plus Operator
Declare String Variables
A string variable is when zero or more characters are enclosed in quotes.
Numbers can also be a string if enclosed in quotes.
let character = "Tempest Cleric"
Escaping Literal Quotes in Strings
Escaping literal quotes is used when you need to use quotes inside of the quotes of the declared string.
This is done using a backslash before both of the actual quotations.
let magicAxe = "Greataxe \"of the Brute\"";
console.log(magicAxe)
Greataxe "of the Brute"
Quoting Strings with Single Quotes
When quoting, single quotes or double quotes may be used.
If this is done, care needs to be taken to avoid ending the string with a non-paired single quotation character.
Again the backslash can be used before the quotation character to escape it and prevent the error.
let myTeam = 'The paladin said "I am the strongest"!'
console.log(myTeam)
The paladin said, "I am the strongest"!
Escape Sequences in Strings
There are also some special characters that we need to use the backslash to escape.
When we do this, make sure not to add any spaces between escape sequences or words.
Below is a list of some of these possible characters;
\' single quote
\" double quote
\ backslash
\n newline
\r carriage return
\t tab
\b word boundary
\f form feed
var character = "Druid\nLevel One";
console.log(character)
Druid
Level One
Concatenating Strings with Plus Operator
Concatenating strings use the (+) concatenate operator to add strings together. This is used to build a new string. There will be no spaces unless specifically added.
var character = "Moon " + "Druid";
console.log(character)
Moon Druid
Top comments (0)