Web Analytics

Advanced String Methods

Modern ~15 min read

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.
HTML
CSS
JS

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).
HTML
CSS
JS

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)
HTML
CSS
JS

Summary

  • Use includes, startsWith, endsWith for easier searching.
  • Use padStart and padEnd for formatting strings.
  • Use Template Literals for cleaner string construction.

Quick Quiz

Which method pads a string from the beginning?

A
padLeft()
B
padBegin()
C
padStart()
D
padFront()