@rbxts/jsnatives
Object.is
Determines whether two values are the same value.
Signature
function is<T, U>(obj: T, other: U): obj is T & U
Description
The Object.is()
method determines whether two values are the same value. It performs a reference check rather than a content equality check.
Unlike the equality operators (==
and ===
), Object.is()
treats NaN
as equal to itself and treats -0
and +0
as different.
The method returns a type predicate that narrows the type to T & U
when the objects are the same.
Examples
Basic reference check
const objA = {};const objB = {};
print(Object.is(objA, objB)); // false (different objects)
Same reference
const objA = {};const objB = objA;
print(Object.is(objA, objB)); // true (same reference)
Nested references
const objA = {};const objB = {ref: objA};
print(Object.is(objA, objB.ref)); // true (same reference)
With primitive values
print(Object.is(1, 1)); // trueprint(Object.is("hello", "hello")); // trueprint(Object.is(true, true)); // trueprint(Object.is(null, null)); // trueprint(Object.is(undefined, undefined)); // true
print(Object.is(NaN, NaN)); // true (unlike ===)print(Object.is(0, -0)); // false (unlike ===)
Type narrowing with is
function processValues<T, U>(value1: T, value2: U) { if (Object.is(value1, value2)) { // In this block, value1 and value2 are typed as T & U return "Values are the same"; } else { // In this block, value1 is T and value2 is U return "Values are different"; }}