Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Array from() Method
Next article icon

JavaScript Array from() Method

Last Updated : 12 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Array.from() method is used to create a new array from any iterables like array, objects, and strings.

JavaScript
const a = Array.from("GeeksforGeeks"); console.log(a); 

Output
[   'G', 'e', 'e', 'k',   's', 'f', 'o', 'r',   'G', 'e', 'e', 'k',   's' ] 

 Syntax

Array.from(object, mapFunction, thisValue);

Parameters

  • object: This parameter holds that object that will convert into an array
  • mapFunction: This parameter is optional and used to call on each item of the array.
  • thisValue: This parameter is optional, it holds the context to be passed as this is to be used while executing the mapFunction. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Return value

It returns a new array from the iterable passed as argument.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Creating Array from Set

We can use Array.from() method to create or convert the given set to the array.

JavaScript
const s = new Set([10, 20, 30]); const a = Array.from(s); console.log(a); 

Output
[ 10, 20, 30 ] 

Creating Array from Object

It can create array from the keys, values and also from each entry of the object.

JavaScript
const obj = { a: 10, b: 20, c : 30, d : 40, e : 50 };  const a1 = Array.from(Object.keys(obj)); const a2 = Array.from(Object.values(obj)); const a3 = Array.from(Object.entries(obj));  console.log(a1); console.log(a2); console.log(a3); 

Output
[ 'a', 'b', 'c', 'd', 'e' ] [ 10, 20, 30, 40, 50 ] [ [ 'a', 10 ], [ 'b', 20 ], [ 'c', 30 ], [ 'd', 40 ], [ 'e', 50 ] ] 

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Next Article
JavaScript Array from() Method

K

Kanchan_Ray
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • JavaScript-Methods

Similar Reads

    JavaScript Int8Array from() Method
    The Javascript Int8Array represents an array of twos-complement of 8-bit signed integers. By default, the contents of Int8Array are initialized to 0. from the () function of Int8Array used to create a new Int8Array from an array-like or iterable object. So when you want to convert an arrayLike or it
    2 min read
    JavaTuples fromArray() method
    The fromArray() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the array, given as parameters. This method can be used to any tuple class object of javatuples library. It is a static function in each javatuple class and it returns the tuple
    2 min read
    ArrayList toArray() method in Java with Examples
    The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a para
    3 min read
    Java Collection toArray() Method
    The toArray() method of Java Collection returns an array containing elements that are inside the calling collection. This article will discuss the toArray() method, its syntaxes, how it works, and some code examples. Syntax of toArray() MethodObject[] toArray();Return Type: The return type of the ab
    3 min read
    Stack toArray() method in Java with Example
    The toArray() method of Stack class in Java is used to form an array of the same elements as that of the Stack. Basically, it copies all the element from a Stack to a new array. Syntax: Object[] arr = Stack.toArray() Parameters: The method does not take any parameters. Return Value: The method retur
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences