Thursday, October 16, 2025
HomeLanguagesJavascriptHow to check if the provided value is of the specified type...

How to check if the provided value is of the specified type in JavaScript ?

The following approach covers how to check if the provided value is of the specified type in JavaScript.

Approach: The Object.is() JavaScript method introduced in ECMAScript 2015 (popularly called ES6) that determines whether two values are the same value. 

Object.is() determines whether two values are the same value. Two values are the same if both are undefined, both null, both true or both false, both strings of the same length with the same characters in the same order, both the same object (meaning both values reference the same object in memory), both numbers and both +0, both -0, both NaN, or both non-zero and both not NaN and both have the same value

Syntax : 

Object.is(value1, value2);

Parameters: This method accepts the following two parameters:

  • value1: It is the first value to compare.
  • value2: It is the second value to compare.

Return Value: This function returns the boolean value.

Example 1: 

Javascript




// Evaluation result is the same as using ===
console.log(Object.is(25, 25));             // true
console.log(Object.is('foo', 'foo'));         // true
console.log(Object.is('foo', 'bar'));         // false
console.log(Object.is(null, null));         // true
console.log(Object.is(undefined, undefined)); // true
console.log(Object.is([], []));             // false
  
var foo = { a: 1 };
var bar = { a: 1 };
  
console.log(Object.is(foo, foo));             // true
console.log(Object.is(foo, bar));             // false


Output:

true
true
false
true
true
false
true
false

Example 2: 

Javascript




// Evaluation result is the same as using ===
console.log(Object.is(25, 25));             // true
console.log(Object.is('foo', 'foo'));         // true
console.log(Object.is('foo', 'bar'));         // false
console.log(Object.is(null, null));         // true
console.log(Object.is(undefined, undefined)); // true
console.log(Object.is([], []));             // false
  
var foo = { a: 1 };
var bar = { a: 1 };
  
console.log(Object.is(foo, foo));             // true
console.log(Object.is(foo, bar));             // false


Output:

false
false
true
true
true
true

Key Difference Between Object.is() and === (Strict Equality)

  • Object.is(+0,0) is false, Object.is(NaN,NaN) is true.
  • So Object.is() is just === with different behavior for negative zero -0 and NaN.

Note: As an ES6 feature, it may not be supported by older browsers, but it can be transpired using Babel for compatibility.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS