skip to Main Content

I’m pretty new to javascript and I wasn’t able to make an if doesn't include statement without changing my array to const which ruins it. I was wondering if there is an option without const

I also tried

if (array.includes("tom")!=true)

and also tried

if (array.includes("tom")==true)

4

Answers


  1. Just add an exclamation point (!) before array to invert the expression array.includes("tom"). You can read it as "if array does not include ‘tom’"

    let array1 = ["john", "joe", "amy"];
    let array2 = ["rudy", "tom", "jeffrey"];
    
    if (!array1.includes("tom")) {
      console.log("array1 does not include 'tom'");
    } else {
      console.log("array1 includes 'tom'");
    }
    
    if (!array2.includes("tom")) {
      console.log("array2 does not include 'tom'");
    } else {
      console.log("array2 includes 'tom'");
    }
    Login or Signup to reply.
  2. if (!array.includes("tom")) { ... }
    

    I’m not sure why your array needs to be const, it should not affect the behavior of the includes() method. But since the method returns a boolean, to get the logical inverse of a boolean value, you can use the ! (NOT) operator in front of the value. Thus, !array.includes("tom") is equivalent to saying "does the array NOT include the string ‘tom’".

    Login or Signup to reply.
  3. The Array.prototype.includes() method is used to determine whether an array includes a certain value among its elements, and returns either true or false. If the array includes the value, it will return true; otherwise, it will return false.

    Here’s how you would use it:

    let array = ["john", "jane", "bob"];
    
    if (array.includes("tom")) {
        console.log("Tom is in the array.");
    } else {
        console.log("Tom is not in the array.");
    }
    

    In this case, because "tom" is not in the array, it will log "Tom is not in the array." to the console.

    Note that declaring array with let or const doesn’t change how includes() works. The difference between let and const is that let allows you to reassign the variable to a new value, while const does not. However, both let and const allow you to change the elements within the array, like adding or removing elements.

    If you want to test if an array does not include a certain value, you can use ! (the logical NOT operator) to negate the result of includes():

    let array = ["john", "jane", "bob"];
    
    if (!array.includes("tom")) {
        console.log("Tom is not in the array.");
    } else {
        console.log("Tom is in the array.");
    }
    

    In this case, it will log "Tom is not in the array." to the console.

    Login or Signup to reply.
  4. Array.prototype.includes() method is used to determine whether the assigned array contains the value mentioned or not and will return a boolean value as True or False as the returned output.

    For Example: arr.includes("tom") : arr is an array and if that array contains the value "tom" it will return true or else will return false.

    So, in your code you can write the if conditions in various manner:

    1. if (array.includes("tom") !== true) or if (array.includes("tom") === true) , making sure it is a strict equality.

    2. if (!array.includes("tom")) or if (array.includes("tom")) , as we know that the return form the includes() method is a true or false boolean value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search