-2

What I essantily want to do is compare all values in array to 123 in an if statment

let values = [1, 2, 3, 123]
if (123 == values.forEach(e => {return e})){

currently, I'm getting an error:

values.forEach(e => {return e})) is not a function

ADyson
  • 57,178
  • 14
  • 51
  • 63
LAYTOR
  • 381
  • 4
  • 12
  • 2
    ForEach doesn't return a value. The comparison needs to be done inside the callback. P.s. don't abbreviate or paraphrase errors when posting...always paste the error in full, and mention the line it occurs on if its not obvious – ADyson Mar 20 '22 at 09:06
  • Please explain and paste your error in question too. – localhost Mar 20 '22 at 09:06

3 Answers3

3

If i understand that you want, try something like this

let values = [1, 2, 3, 123]

if (values.includes(123)) {
   *do some code*
}
Aaron Vasilev
  • 438
  • 2
  • 6
2

const array1 = [1,2,123,123];

array1.forEach(element => element === 123 && console.log(element));
Ala
  • 31
  • 3
2

You can use array.indexOf() function to check whether 123 is present in the array or not :

let values = [1, 2, 3, 123]

    if (values.indexOf(123)!=-1) {
       ...write code    
    }

let values = [1, 2, 3, 123]

if (values.indexOf(123)!=-1) {
   console.log("123 is present");
}else {
  console.log("123 is not present");
}
Hritik Sharma
  • 1,756
  • 7
  • 24