How to Ignore case of startsWith, endsWith Methods in Javascript

In this tutorial you will learn how to string Ignore case of startsWith, endsWith Methods in JavaScript. In JS we can use the startsWith and endWith methods to ignore the case.

When we use startsWith method we need to convert the entire string and the substring to lowercase when calling the method.

// Not Supported in IE 6-11
const str = 'Coding Driver';
const substr = 'CODI';

console.log(str.toLowerCase().startsWith(substr.toLowerCase())); // true


if (str.toLowerCase().startsWith(substr.toLowerCase())) {
  // string starts with substring
}

Same as startsWith we use startsWith method we need to convert the entire string and the substring to lowercase when calling the method.

// Not Supported in IE 6-11
const str = 'Coding driver';
const substr = 'IVER';

console.log(str.toLowerCase().endsWith(substr.toLowerCase())); // true


if (str.toLowerCase().endsWith(substr.toLowerCase())) {
  // string ends with substring
}

Note: If you have to support any of the versions of Internet Explorer, it’s best to use a polyfill or babel to compile your code to a version of JavaScript the browser can understand.