JavaScript Glossary: String includes()

Publikováno: 19.3.2019

Basics

The includes method checks a provided string against the calling String object, it returns true if the calling String object contains the provided string a...

Celý článek

Basics

The includes method checks a provided string against the calling String object, it returns true if the calling String object contains the provided string and false otherwise. If no arguments are provided, the method returns false.

"welcome to scotch".includes("scotch") // true

https://scotch.io/embed/gist/cca84ce9a39cc8c8dbf8bd932d7543ff

The .includes() method is case sensitive

This method:

  • Takes a string as a parameter.
  • Checks the provided string against the calling string object.
  • Returns true if the calling string contains the provided string and false otherwise.

Syntax

const isPresent = string.includes(searchString, startPosition)

2 Parameters

searchString This is a string to be checked against the calling string object - required.

startPosition This is an integer specifying the position within the string where the search begins - optional.

Returns a boolean

Returns a boolean, true if the calling string contains the provided string, false otherwise.

Common Uses and Snippets

Verify a name in comma separated values

The .includes() method can be used to search a list of comma separated values in a string.

https://scotch.io/embed/gist/92c45fc9d8b8d4c2af57bc573baa81f0

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace