JavaScript - Check if All Array Values Are Equal Last Updated : 23 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Here are the different methods to check if all values of an array are equal or not in JavaScript1. Using Array.every() methodFirst, get the array of elements. Pass it to an arrow function, which calls every() method on each array element and returns true if each element matches the first element of the array. JavaScript let a = [1, 1, 1, 1]; const all = a => a.every(v => v === a[0]); console.log(all(a)); Outputtrue In this exampleThe all() function checks if all elements in the array are equal to the first element using the every() method.In this case, it returns true because all elements in [1, 1, 1, 1] are equal to the first element (1)2. Using Array.reduce() methodFirst, get the array of elements. Pass it to a function called the reduce() method on the array element. Return true if each element matches the first element of the array. JavaScript let a = ["G", "F", "I", "J"]; function all(a) { if (!a.length) return true; return a.reduce(function (a, b) { return (a === b) ? a : (!b); }) === a[0]; } console.log(all(a)); Outputfalse In this example:The all() function uses reduce() to compare all elements of the array a with each other and checks if they are all equal.It returns false because the elements in a (["G", "F", "I", "J"]) are not all equal.3. Using SetTo check if all elements in an array are equal, we can pass the array to the Set constructor. Since a Set only stores unique elements, if the size of the set is 1, it indicates that all elements in the array are equal. JavaScript let a = [1, 1, 1, 1]; function all(a) { const res = new Set(a).size === 1; return res; } console.log(all(a)); Outputtrue In this exampleThe all() function converts the array into a Set, which removes duplicate elements, and checks if the set's size is 1.It returns true because all elements in the array [1, 1, 1, 1] are the same, so the set contains only one unique element.4. Using for...of loopThe for...of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …,etc) and will check the element is same as the first element. JavaScript const a1 = [1, 1, 1, 1]; const a2 = [1, 1, 2]; function all(a) { let eq = true; for (const element of a) { if (element !== a[0]) { eq = false; break; } } return eq; } console.log(all(a1)); console.log(all(a2)); Outputtrue false In this exampleThe all() function checks if all elements in the array a are equal to the first element.It loops through each element in a and compares it with the first element (a[0]).If any element is different, it sets eq to false and breaks the loop.The function returns true if all elements are the same and false otherwise.5. Using Array.some() MethodThe Array.some() method to determine if any element in the array does not match the first element. If the method returns false, it means all elements are equal. JavaScript function elem(a) { if (a.length === 0) return true; const b = a[0]; return !a.some(element => element !== b); } const a1 = [1, 1, 1, 1]; const a2 = [1, 2, 1, 1]; console.log(elem(a1)); console.log(elem(a2)); Outputtrue false In this exampleThe elem() function checks if all elements in the array arr are equal to the first element by using some() to find any element not equal to the first one.It returns true for arr1 (all elements are equal) and false for arr2 (not all elements are equal). Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Like