AI Hints
Home›JavaScript Tutorial›Names, Numbers and Strings
JavaScript · Lesson 9 of 138

Strings

Text in quotes.

⏱ 2 min read▶ Video at 8:55🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Text in a program is called a string, because it's a string of characters. Names, messages, and web addresses are all strings. You make one by putting text inside quotes.

Strings
From the video: Strings
  • Text is a string
  • Names, messages, addresses
  • Inside quotes

Joining strings

Plus joins strings together. That's called concatenation. Notice the space inside the quotes, or the words would run together. Length tells you how many characters a string has. And square brackets get a single character, counting from zero. Run it.

app.jsjavascript
const first = "Ada";
const last = "Lovelace";
const full = first + " " + last;

console.log(full);
console.log("length:", full.length);
console.log("first letter:", full[0]);
Output
Ada Lovelace
length: 12
first letter: A

Quotes inside strings

To put a quote mark inside a string, use the other kind of quote around it. Or put a backslash in front, which is called escaping. Backslash N makes a new line. Strings with plus can get messy, so next we'll learn a much nicer way to build them.

keyvalue
"It's fine"single inside double
'Say "hi"'double inside single
\nnew line
+joins strings

Length and characters

Every string has a length, the number of characters in it, including spaces. You can read a single character with square brackets, counting from zero. So index zero is the first character. And length minus one gives the last one. Run it.

app.jsjavascript
const word = "JavaScript";

console.log(word.length);
console.log(word[0]);
console.log(word[4]);
console.log(word[word.length - 1]);
console.log("Hi there".length);
Output
10
J
S
t
8

Summary

Strings hold text. Plus joins them, length counts characters, and square brackets pick one character. Next, template literals.

Key takeaways
  • Quotes make strings
  • + joins
  • str[0] is the first
Next up: Template Literals — backticks and ${}