Web Analytics

JavaScript Strings

Beginner ~15 min read

What are Strings?

Strings are used for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes.

HTML
CSS
JS

String Methods

JavaScript provides many useful methods to work with strings.

HTML
CSS
JS

Template Literals

Template literals (introduced in ES6) use backticks (`) rather than quotes to define strings. They allow for:

  • Multi-line strings
  • String interpolation (variables inside strings)
HTML
CSS
JS

String Search

Methods for searching strings:

  • indexOf(): Returns the index of the first occurrence
  • lastIndexOf(): Returns the index of the last occurrence
  • includes(): Returns true if string contains a value
  • startsWith(): Returns true if string starts with value
  • endsWith(): Returns true if string ends with value

Summary

  • Strings are for storing text.
  • Use single or double quotes for literals.
  • Use backticks for template literals (recommended for dynamic strings).
  • Strings have many built-in methods like slice(), replace(), and toUpperCase().
  • Strings are immutable (methods return new strings, they don't change the original).

Quick Quiz

Which symbol is used for template literals?

A
' (Single Quote)
B
" (Double Quote)
C
` (Backtick)
D
- (Hyphen)