DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • TypeScript: Useful Features
  • useState() vs. useRef(): Understand the Technical Difference
  • A Comprehensive Guide on JavaScript Array Map Method
  • JSON Minify Full Guideline: Easy For You

Trending

  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Infrastructure as Code (IaC) Beyond the Basics
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  1. DZone
  2. Data Engineering
  3. Data
  4. Using an Array as Function Parameter in JavaScript

Using an Array as Function Parameter in JavaScript

Let's look at how function parameters can be defined as arrays in Javascript.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Jun. 30, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

In Javascript, we often have data stored as arrays, and functions we want to call. Sometimes, the data in our arrays is exactly the data we want to pass to a function. Fortunately, there are ways in Javascript to use arrays as the input values for functions. Let's look at how to use arrays as function parameters.

How to Use Arrays as Function Parameters

When we have a function we want to pass an array to, the most basic way to do it would be like this:

JavaScript
 
let numbers = [ 1, 2, 3 ]
let myFunction = (x, y, z) => {
    return x + y + z;
}

// Returns 6
let getCalculation = myFunction(numbers[0], numbers[1], numbers[2]);

Of course, this can be quite annoying, especially when working with functions that have very long lists of properties. As such, Javascript provides us with two ways to use arrays as function parameters in Javascript - apply() and the spread operator.

Passing Arrays to Functions With the Spread Operator

The modern, and easiest way to pass an array to a function is to use the spread operator. The spread operator (...) is simply added before the array. Using our previous example, it looks like this:

JavaScript
 
let numbers = [ 1, 2, 3 ]
let myFunction = (x, y, z) => {
    return x + y + z;
}

// Returns 6
let getCalculation = myFunction(...numbers);

We can also directly add the array into the function, without needing a variable. For example:

JavaScript
 
let myFunction = (x, y, z) => {
    return x + y + z;
}

// Returns 6
let getCalculation = myFunction(...[ 1, 2, 3 ]);

If you want to learn everything the spread operator can do, check out my guide on that here.

Passing Arrays to Functions Using Apply()

The other way to do this is to use apply(). If you're unfamiliar with the main use case of apply(), check out my full guide on Javascript's this object here.

Ultimately, apply() lets us take a function, and pass an array into it. The first attribute of apply() actually refers to the this object we want to use on the function. The second argument is an array of all the parameters for that function. That means you can define the structure of this as well as pass an array into the function itself.

This obviously has some specific benefits when compared to the spread (...) operator, so it may be more suitable depending on what you want to achieve. Using our previous example, we can pass an array into our function like so:

JavaScript
 
let myFunction = (x, y, z) => {
    return x + y + z;
}

// Returns 6
let getCalculation = myFunction.apply({}, [ 1, 2, 3 ]);

Here, I am leaving the this object empty, so if we did use this in myFunction, it'd be an empty object - that's what the first argument of apply() does. The second is our array [1, 2, 3], referring to x, y, and z respectively.

Data structure JavaScript

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • useState() vs. useRef(): Understand the Technical Difference
  • A Comprehensive Guide on JavaScript Array Map Method
  • JSON Minify Full Guideline: Easy For You

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!