JavaScript Glossary - Lesson #21

JavaScript String .includes() Method

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
// New string to test
let string = "are you available for the meetup?"

const searchWord = "meetup"

const isPresent = string.includes(searchWord)

console.log(isPresent) // true

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.
const isPresent = string.includes(searchString, startPosition)

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, true if the calling string contains the provided string, false otherwise.

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

// List of names
const names = "mike, chris, perry, john, liz"

// Name to be searched
let name = "chris"

// Search for name
let isPresent = names.includes(name)

// Log result
console.log(isPresent) // true

Want to improve your coding and design skills?

I'm continually researching the best practices and tools for coding.
Join 50,000+ developers looking to make cool stuff.

We value your privacy. 1-click unsubscribe.

Chris Sev

Chris Sev

Chris Sev is the co-founder of Better Dev. Coding better every day. Previously he created which was acquired.


What did you think of the article? Let us know!
(these comments are powered by GitHub issues and use 0 trackers)