Value Is Array-Like

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore the concept of array-like objects in JavaScript and learn how to determine whether an object is iterable. We will use the isArrayLike function to check if an object has an iterator and therefore can be iterated over using a for...of loop. By the end of this lab, you will have a better understanding of how to work with array-like objects in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) 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`") javascript/ToolsandEnvironmentGroup -.-> javascript/bom("`Browser Object Model`") subgraph Lab Skills javascript/variables -.-> lab-28410{{"`Value Is Array-Like`"}} javascript/data_types -.-> lab-28410{{"`Value Is Array-Like`"}} javascript/arith_ops -.-> lab-28410{{"`Value Is Array-Like`"}} javascript/comp_ops -.-> lab-28410{{"`Value Is Array-Like`"}} javascript/bom -.-> lab-28410{{"`Value Is Array-Like`"}} end

Check if Value is Array-Like

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

  1. Open Terminal/SSH.
  2. Type node.
  3. Use the following code to check if the provided argument is iterable:
const isArrayLike = (obj) =>
  obj != null && typeof obj[Symbol.iterator] === "function";
  1. The function will return true if the provided argument is an array-like object, and false otherwise.
  2. For example:
isArrayLike([1, 2, 3]); // true
isArrayLike(document.querySelectorAll(".className")); // true
isArrayLike("abc"); // true
isArrayLike(null); // false

Summary

Congratulations! You have completed the Value Is Array-Like lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like