JavaScript Strings

Learn about JavaScript strings.

By the end of this lesson, we’ll be able to do the following:

  • Use new ES6 string methods.
  • Apply the template literal syntax.

Strings

ES6 strings offer new string methods, template literals, and tagged template literals.

New string methods

ES6 offers a few new string methods:

  • The .repeat(number) method repeats a given string a specified number of times.

  • The .startsWith(searchString, position) method returns true if a string starts with searchString. The position parameter specifies the start position of the search, with the default value being 0.

  • The .endsWith(searchString, length) method returns true if a string ends with searchString. The length parameter specifies the length of the string to search.

  • The .includes(searchString, position) method returns true if a given string can be found in another string.

Template literals

Template literals allow us to embed expressions in our strings using backticks (`) and ${express}. This can help simplify the syntax string concatenation and create multiline strings. For example, see the code below:

Get hands-on with 1200+ tech skills courses.