JavaScript from() | Get an Array from an Iterable Object

The JavaScript from() method returns an array from an array-like or iterable object. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
  
   <p id="xyz"></p>

   <script>
      let str = "codescracker";
      const arr = Array.from(str);
      document.getElementById("xyz").innerHTML = arr;
   </script>
   
</body>
</html>
Output

Note - The from() is a static method. Static methods are called by classes, whereas dynamic methods are called by objects.

JavaScript from() Syntax

The syntax of from() method in JavaScript is:

Array.from(object, mapFn, thisArg)

The object refers to an array-like or iterable object, that is going to convert into an array.

The mapFn refers to a map function. Use this parameter, when you need to call a function on every element of the specified array.

The thisArg refers to the value that will be used as this for the mapFn.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋