Thoughts on

A blog of various opinions and ideas on development

Demos subscribe via Podcast

Javascript: includes(...) vs indexOf(...)


If you are using Array.prototype.indexOf(...) >= 0 or String.prototype.indexOf(...) >= 0 you might want to use Array.prototype.includes(...) >= 0 or String.prototype.includes(...) instead.


.includes(...) became available with ES 2016, before then all we had was .indexOf(...) >= 0, and that’s how you would check if an object exists in array-like structures.

IE doesn’t have support for .includes(...) so you would have to use a polyfill or .indexOf(...).

const nums = [1, 2, 3];
console.log(nums.indexOf("a") >= 0); //false
console.log(nums.includes("a"));     //false

console.log(nums.indexOf(2) >= 0);   //true
console.log(nums.includes(2));       //true

const str = "A E I O U a e i o u";
console.log(str.indexOf(1) >= 0);    //false
console.log(str.includes(1));        //false

console.log(str.indexOf("a") >= 0);  //true
console.log(str.includes("a"));      //true
tags: Programing - GeneralAdvice - Javascript - Always