w3resource

JavaScript: Get a boolean determining if the passed value is an object or not

JavaScript fundamental (ES6 Syntax): Exercise-194 with Solution

Write a JavaScript program to get a boolean determining if the passed value is an object or not.

  • Uses the Object constructor to create an object wrapper for the given value.
  • If the value is null or undefined, create and return an empty object.
  • Otherwise, return an object of a type that corresponds to the given value.

Sample Solution:

JavaScript Code:

//#Source https://bit.ly/2neWfJ2 
const isObject = obj => obj === Object(obj);

console.log(isObject([1, 2, 3, 4]));
console.log(isObject([]));
console.log(isObject(['Hello!']));
console.log(isObject({ a: 1 }));
console.log(isObject({}));
console.log(isObject(true));

Sample Output:

true
true
true
true
true
false

Flowchart:

flowchart: Get a boolean determining if the passed value is an object or not

Live Demo:

See the Pen javascript-fundamental-exercise-194 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to check if a value is object-like. Check if the provided value is not null and its typeof is equal to 'object'.
Next: Write a JavaScript program to check if a given argument is a number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

How to check whether a string contains a substring in JavaScript?

ECMAScript 6 introduced String.prototype.includes:

const string = "foo";
const substring = "oo";

console.log(string.includes(substring));

includes doesn't have Internet Explorer support, though. In ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found:

var string = "foo";
var substring = "oo";

console.log(string.indexOf(substring) !== -1);

Ref: https://bit.ly/3fFFgZv