JavaScript Strings
What are Strings?
Strings are used for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes.
String Methods
JavaScript provides many useful methods to work with strings.
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)
String Search
Methods for searching strings:
indexOf(): Returns the index of the first occurrencelastIndexOf(): Returns the index of the last occurrenceincludes(): Returns true if string contains a valuestartsWith(): Returns true if string starts with valueendsWith(): 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(), andtoUpperCase(). - Strings are immutable (methods return new strings, they don't change the original).
Quick Quiz
Which symbol is used for template literals?
Enjoying these tutorials?