Skip to content

Instantly share code, notes, and snippets.

@gaearon
Last active July 8, 2024 06:18
Show Gist options
  • Save gaearon/08a85a33e3d08f3f2ca25fb17bd9d638 to your computer and use it in GitHub Desktop.
Save gaearon/08a85a33e3d08f3f2ca25fb17bd9d638 to your computer and use it in GitHub Desktop.
strictEquals.js
// Your scientists were so preoccupied
// with whether or not they could,
// they didn't stop to think if they should.
// Like a === b
function strictEquals(a, b) {
if (Object.is(a, b)) {
// Same value.
// Is this NaN?
if (Object.is(a, NaN)) { // We already know a and b are the same, so it's enough to check a.
// Special case #1.
return false;
} else {
// They are equal!
return true;
}
} else {
// Different value.
// Are these 0 and -0?
if (
(Object.is(a, 0) && Object.is(b, -0)) ||
(Object.is(a, -0) && Object.is(b, 0))
) {
// Special case #2.
return true;
} else {
// They are not equal!
return false;
}
}
}
@betomadrazo
Copy link

betomadrazo commented Jun 3, 2023

My version:

function strictEquals(a, b) {
    if (Number.isNaN(a) && Number.isNaN(b)) return !Object.is(a, b);
    if ((Object.is(a, -0) ^ Object.is(b, -0)) && (Object.is(a, 0) ^ Object.is(b, 0))) return !Object.is(a, b);
  return Object.is(a, b);
}

@sergiosrtd
Copy link

Hope this is the shortest

function strictEquals(a,b){
  if(Number.isNaN(a) && Number.isNaN(b)) return false
  if(!Object.is(a,b) && Object.is(a+"",b+"")) return true
  return Object.is(a,b)
}

@marnauortega
Copy link

function stringEquals(a, b) {
    if (isNaN(a)) return false
    if ([0, -0].includes(a) && Object.is(a, -b)) return true
    return Object.is(a, b)
}

First special case NaN === NaN => both a and b will be NaN and therefore, and if either value is NaN, should return false. Other case, return the result of Objects.is(a, b)

Second sepecial case 0 === -0 or -0 === 0 => if the a is 0 or -0 and Object.is(a -b), its one of the special situations, that needs to return true. Otherwise, return Object.is(a, b)

I found it interesting that you managed to shorten if (isNaN(a) && isNaN(b)) return false to if (isNaN(a)) return false. You don't need to check if either value is NaN, you have enough with checking if the first value is NaN, in which case it will definitely evaluate to false. And you don't need to care about the remaining case in which b is NaN while a isn't, because Object.is() will handle it perfectly.

@Yinboan
Copy link

Yinboan commented Aug 30, 2023

 function strictEquals(a,b){
     if(typeof a === 'number' && typeof b === 'number'){
         return a == b   
     }
     return Object.is(a,b)
 }

@spcbfr
Copy link

spcbfr commented Sep 27, 2023

I couldn't figure it out on my own so I looked at your version and tried to minimize (not simplify!) it to the best of my ability

const strictEquals = (a,b) => (Object.is(a,b) 
     ? !Object.is(a, NaN) 
     : (Object.is(a, 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0)))
}

@mhmdsalahsebai
Copy link

function strictEquals(a, b) {
    if(Object.is(a, NaN) && Object.is(b, NaN)) return false;
    if(Object.is(a, -0) && Object.is(b, 0)) return true;
    if(Object.is(b, -0) && Object.is(a, 0)) return true;
    return Object.is(a, b);
}

@clark-cui
Copy link

clark-cui commented Jun 21, 2024

function strictEquals(a,b){
    if(Number.isNaN(a)&&Number.isNaN(b)){
        return false;
    }
    if(Number.isFinite(a) && Number.isFinite(b) && Math.abs(a)===0 && Math.abs(b)===0){
        return true;
    }
    return Obejct.is(a,b);
}

@Ris345
Copy link

Ris345 commented Jul 4, 2024

const strictEquals = (a, b) => {
        if (Object.is(a, NaN) && Object.is(NaN, b)) {
                return false; 
        } else if (Object.is(a, 0) && Object.is(a, -0)) {
                return true 
       } else if (Object.is(a, -0) && Object.is(b, 0)) {
                return true 
        } 
            return Object.is(a,b) 
        
}

@Saran-73
Copy link

Saran-73 commented Jul 5, 2024

const strictEquals = (a, b) => {
  const isSameValue = Object.is(a, b)
  // === behaves as same as Object.is() checks the both args are of same value or not
  // edge cases 
  // 0 === -0 || -0 === 0 // true
  // NaN === NaN // false 
  
  if(isSameValue && Object.is(a, NaN)){
      return false
   }
  if((Object.is(a , 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0))){
    return true
  }
  
 return isSameValue
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment