Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a an object jsonRes[0] containing values which need to be removed based on a condition. The following works to remove null , missing values and those equal to zero in the stringified object:

function replacer(key, value) {
          // Filtering out properties
          if (value === null || value === 0 || value === "") {
            return undefined;
          return value;
JSON.stringify(jsonRes[0], replacer, "\t")

However, when I add a condition using the the includes method, I receive an error:

function replacer(key, value) {
          // Filtering out properties
          if (value === null || value === 0 || value === "" || value.includes("$")) {
            return undefined;
          return value;
Uncaught TypeError: value.includes is not a function

Why is this the case and is there a workaround?

If you look at the documentation of includes() :developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, most of the browsers don't support this property. stackoverflow.com/questions/31340868/… You can use widely supported indexOf() after converting the property to string using toString(): – S M Jan 24, 2017 at 5:45 Well, if value is a number, (non-array) object or boolean, it won't have an .includes method? Presumably you meant to write something like typeof value == "string" && value.includes("$"). – Bergi Jan 24, 2017 at 5:46

You can use String.indexOf() instead of String.includes, As it is available in ES6 and not supported in IE at all.

typeof value == "string" && value.indexOf('$') > -1

Also note if value is not string type it will still raise an error boolean, Number doesn't the the method. You can use typeof to validate whether value is a string.

The .includes() API is part of the String and Array data type.

So what the error is trying to tell you is that the value for variable value, e.g. an integer or object, does not have the property .includes.

You could do checks like

  • typeof a_string === 'string'
  • an_array instanceof Array
  • before the .includes() api to prevent this.

    Obviously this will make your if statement rather ugly due to the number of checks you have.

    Based on the way your code is written I suspect you are more interested in checking "String" than array. So becareful of arrays. Your code may not work properly if it is array.

    Anyway here is a refractored version of your code.

    function replacer(key, value) {
       // Filtering out properties
       if (!value || typeof value === "string" && value.includes("$")) {
            return undefined;
       return value;
    console.log("NULL returns:" + replacer('test', null));
    console.log("$Test returns:" + replacer('test', '$test'));
    console.log("Blah returns:" + replacer('test', 'Blah'));

    I solved this error, which I was getting when applying "includes" to a "window.location" value, by appending ".toString();"

    var requestUrl = window.location.toString();

    if (requestUrl.includes(urlBase + "#")) {

    I actually am not sure what type of the variable named value is, but anyway, Array.prototype.includes and String.prototype.includes are only available in ES6. You need to use babel-polyfill or any other bundling modules like rollup.js, webpack with babel or something like that to use includes function.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.