Text and Strings

Working with words and joining them together

8 min read · runnable Javascript

A piece of text in programming is called a string. You can think of it as a string of characters (letters, spaces, numbers, and symbols) threaded together.

You always wrap a string in quotes. Both double quotes "like this" and single quotes 'like this' work, as long as you match them.

let greeting = "Hello";
let name = 'Alex';

Joining strings together

You can stick strings together using the + sign. This is called concatenation, which is a fancy word for joining end to end.

let first = "Good";
let second = "morning";
console.log(first + " " + second);

Notice the " " in the middle: that is a string containing a single space, so the words do not get squashed together.

Template strings (an easier way)

Joining with lots of + signs gets messy. JavaScript offers a tidier way using backticks ( ` ) and ${ } to drop a variable straight into the text:

let name = "Alex";
console.log(`Hello, ${name}!`);
Tip: The backtick key is usually in the top-left corner of your keyboard, above Tab. Inside backticks, anything written as ${ } gets replaced by the value of that variable.

Try it yourself

Run the code and then change the name to your own.

Try it yourself
Output
Press “Run code” to see the result.

Quick check

Q1 What is a string?

Q2 What does console.log("Cat" + "dog") print?

Want to save your progress? It's free.

Create a free account