Introduction
In this lab, we will be exploring how to check if a value is object-like using JavaScript. We will be using the isObjectLike function to determine if a given value is an object or array, and we will also learn how to differentiate it from other data types such as null or functions. By the end of this lab, you will have a better understanding of object-like values and how to work with them in your JavaScript code.
Checking if a Value is Object-Like
To check if a value is object-like, follow these steps:
- Open the Terminal/SSH.
- Type
nodeto start practicing coding. - Check if the provided value is not
nulland itstypeofis equal to'object'.
Here's the code you can use:
const isObjectLike = (val) => val !== null && typeof val === "object";
You can test this function with the following examples:
isObjectLike({}); // true
isObjectLike([1, 2, 3]); // true
isObjectLike((x) => x); // false
isObjectLike(null); // false
Summary
Congratulations! You have completed the Value Is Object-Like lab. You can practice more labs in LabEx to improve your skills.