Advanced String Methods
Searching Strings
Modern JavaScript provides cleaner ways to search within strings than
indexOf.
includes(): Returns true if a string contains a specified value.startsWith(): Returns true if a string begins with a specified value.endsWith(): Returns true if a string ends with a specified value.
String Padding
ES2017 added two String methods to support padding at the beginning and at the end of a string.
padStart(): Pads a string with another string until it reaches a given length (from the start).padEnd(): Pads a string with another string until it reaches a given length (from the end).
Template Literals
Template Literals use back-ticks (``) rather than quotes ("") to define a string. They allow for:
- Multi-line strings
- String interpolation (variables inside strings)
Summary
- Use
includes,startsWith,endsWithfor easier searching. - Use
padStartandpadEndfor formatting strings. - Use Template Literals for cleaner string construction.
Quick Quiz
Which method pads a string from the beginning?
Enjoying these tutorials?