Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function deepEqual(a,b){
  if((typeof a=='object'&& a!=null)&&(typeof b=='object'&& b!=null)){
   if(Object.keys(a).length != Object.keys(b).length){
     return false
   }    
    for(let key in a){
      let aValue = a[key]
      let bValue = b[key]
      
      if (typeof aValue == 'object' && typeof bValue == 'object') {
        // catches nested objects and recursively assesses deep equality
        if(!deepEqual(aValue, bValue)) return false
      }
      else if (aValue !== bValue) {
        // if the values are not objects, catches if they are not equal
        return false
      }
    }
   return true
  }else if(a!==b){
    return false
  }
  else{
    return true
  }
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers