If statement condition - checking if three numbers are the same

I have an array that holds five numbers. I want an if statement to check if the first three numbers in the array are the same. What is the simplest way to write the condition?
Can I do something like this…

let numberArray = [3, 3, 3, 4, 5]
if (numberArray[0] === numberArray[1] === numberArray[2]) {
   console.log("the first three numbers of the array match");
}

Or should I approach it like this…

let numberArray = [3, 3, 3, 4, 5]
if (numberArray[0] === numberArray[1] && numberArray[1] === numberArray[2]) {
   console.log("the first three numbers of the array match");
}

The second. Javascript doesn’t have chained comparison operators the way Python does. If you try the first, you’ll find it doesn’t work, because it’s comparing numberArray[2] to the boolean result of numberArray[0] === numberArray[1]

3 Likes

Thanks for your reply. Even though I’m learning, I’m trying to do things the ‘correct’ or most efficient way and that second option just felt a bit clumsy. Even though it does work. Thanks for your help.