Value Is Object-Like

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28431{{"`Value Is Object-Like`"}} javascript/data_types -.-> lab-28431{{"`Value Is Object-Like`"}} javascript/arith_ops -.-> lab-28431{{"`Value Is Object-Like`"}} javascript/comp_ops -.-> lab-28431{{"`Value Is Object-Like`"}} end

Checking if a Value is Object-Like

To check if a value is object-like, follow these steps:

  1. Open the Terminal/SSH.
  2. Type node to start practicing coding.
  3. Check if the provided value is not null and its typeof is 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.

Other JavaScript Tutorials you may like