Check if string starts or ends with text

Published on September 1st, 2019

javascript icon

It’s as easy as calling startsWith() or endsWith() methods on JavaScript string prototype.

As an example we can use string similar to this.

const string = 'Hey, have a great day!'

And check if string is question with simple function.

const isQuestion = str => {
  return str.endsWith('?')
}

When we pass example string to this function it will, of course, return false.

isQuestion(string) // false

You can use startsWith() same way.