Method to determine if all values in an array are the same.


/ Published in: JavaScript
Save to your folder(s)

Usage:
var a = ['a','a','a'];
var b = a.AllValuesSame(): //true

var a ['1','1','2'];
var b = a.AllValuesSame(); //false


Copy this code and paste it in your HTML
  1. Array.prototype.AllValuesSame = function(){
  2.  
  3. if(this.length > 0) {
  4. for(var i = 1; i < this.length; i++)
  5. {
  6. if(this[i] !== this[0])
  7. return false;
  8. }
  9. }
  10. return true;
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.