• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Check if all elements in an array are equal

Check if all elements in an array are equal

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.

const allEqual = arr => arr.every(val => val === arr[0]);

// Examples
allEqual([1, 2, 3, 4, 5, 6]); // false
allEqual([1, 1, 1, 1]); // true

Source

https://www.30secondsofcode.org/js/s/all-equal

Miscellaneous

Related Snippets:

  • Check if all the elements in values are included in arr
  • Filtering an array of objects based on a condition
  • Looping over an object’s keys and values
  • Template Literals

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers