Design & Download
Your JS Library
in Seconds!!!

Snippet: is() - Test for Equality

Written by Christopher West (cwest) on December 06, 2015.
Tests to see if two or more values are exactly the same. This is similar to using "===" except it can be used to differentiate between 0 and -0 and it will correctly indicate that NaN is NaN.
function is(x, y) {
  x = x === y
    ? x !== 0 || 1 / x == 1 / y // -0 vs 0
    : (x != x && y != y); // NaN
  y = arguments;
  return x && y.length > 2
    ? is.apply(0, slice(y, 1))
    : x;
}

is(...) API Documentation

Description

Tests to see if two or more values are exactly the same. This is similar to === with two differences:
  1. -0 === 0 evaluates to true
    but YourJS.is(-0, 0) evaluates to false
  2. NaN === NaN evaluates to false
    but YourJS.is(NaN, NaN) evaluates to true

Parameters

  1. x {*}:
    The value to be compared to for equality.
  2. y {*}:
    The value to compare to x for equality.
  3. ...opt_otherValues {...*}:
    Optional. If additional values are given they will all be compared to x for equality.

Returns

Returns true if all of the parameters passed in are the same, otherwise false is returned.