Basics: Strings

A string type is a sequence of characters. It’s defined in the source code as a string literal, which is enclosed in quotes or double quotes:

const test = 'A string'

const test2 = 'Another string'

Strings can be joined using the + operator:

const test = "A" + " " + "string"

Template literals are string literals that allow a more powerful way to define strings, using the backtick:

const test = `something`

Using template literals you can perform string substitution with ${}, embedding the result of any JS expression:

const variable = 1

const test = `a string with the number ${variable}`

const test2 = `a string with the number ${variable + 10}`

And they let you create multiline strings:

const test = `a string
defined
on multiple lines`

Strings are a primitive type in JavaScript and as with numbers this means they are passed by value, not by reference.

All strings have some methods that provide important features that JavaScript gives us out of the box, like toUpperCase() to make a string uppercase.

We’ll see them when we’ll talk about the standard library.

Lessons in this unit:

0: Introduction
1: Literals, identifiers and variables
2: Comments
3: The difference between let, const and var
4: Types
5: Operators and expressions
6: Let's start with arithmetic operators
7: The assignment operator
8: Operators precedence
9: ▶︎ Strings
10: Numbers
11: Semicolons, white space and sensitivity
Want to learn more? Check out our courses