Files
30-seconds-of-code/snippets/allEqual.md
Isabelle Viktoria Maciohsek 8cb9469ff4 Re-tag function snippets
2020-10-18 19:42:11 +03:00

19 lines
473 B
Markdown

---
title: allEqual
tags: array,beginner
---
Check if all elements in an array are equal.
- Use `Array.prototype.every()` to check if all the elements of the array are the same as the first one.
- Elements in the array are compared using the strict comparison operator, which does not account for `NaN` self-inequality.
```js
const allEqual = arr => arr.every(val => val === arr[0]);
```
```js
allEqual([1, 2, 3, 4, 5, 6]); // false
allEqual([1, 1, 1, 1]); // true
```