How to return an array in JavaScript?

In this article, we will explore how to return an array from a function in JavaScript.

Apart form that, you’ll discover different methods for creating and manipulating arrays, such as using the Array.from() method, the new Array constructor, or local variables.

And explore real-world examples for returning arrays in JavaScript

Don’t miss out on this opportunity to enhance your JavaScript skills to the next level concerning how to return an array from a function in JavaScript.

What is an Array in JavaScript?

An array is a data structure in JavaScript that can store a collection of values.

Each value in an array is called an element, and the elements are stored in a contiguous block of memory.

Arrays are indexed, meaning that each element has a unique index number that can be used to access it.

The great thing about arrays in JavaScript is that it can hold different types of things, like numbers, words, and even other arrays. It is very flexible and useful for organizing and storing information in our programs.

You can use various methods to manipulate arrays, such as adding or removing elements, sorting the elements, or searching for specific values.

Here’s an example of how to create an array in JavaScript:

let myArray = [1, 2, 3, 4, 5];

In this example, we use the square bracket notation [] to create an array with three elements: 1, 2, 3, 4 and 5.

We can access the elements of the array using their index numbers like this:

console.log(myArray[0]); // 1
console.log(myArray[1]); // 2
console.log(myArray[2]); // 3
console.log(myArray[1]); // 4
console.log(myArray[2]); // 5

Furthermore, Arrays are very helpful and useful for storing and organizing data in your programs. You can use them to store lists of items, keep track of data over time, or perform complex calculations.

How to return array from function in JavaScript?

Here are some different ways to return an array from a function in JavaScript:

Solution 1: Use a local variable

You can create a local variable within the function to store the array, and then return that variable at the end of the function.

The sampleFunction function is defined using the function keyword, followed by the function name and a pair of parentheses (). The code within the curly braces {} defines the behavior of the function.

function sampleFunction() {
…
}

Within the sampleFunction function, a local variable named myArray is declared using the let keyword.

This variable is assigned an array of numbers [1, 2, 3, 4, 5].

function sampleFunction() {
let myArray = [1, 2, 3, 4, 5];
…
}

The return statement is used to specify the value that the function should return when it is called.

In this case, the function returns the value of the myArray variable, which is an array of numbers.

function sampleFunction() {
let myArray = [1, 2, 3, 4, 5];
return myArray;
}

After defining the sampleFunction function, it is called by using its name followed by a pair of parentheses (). The returned value from the function call is assigned to a variable named result.

let result = sampleFunction();

Finally, the value of the result variable is logged to the console using the console.log method.

console.log(result);

When this code is executed, it defines a function that returns an array of numbers, calls that function and assigns its return value to a variable, and then logs that variable to the console.

The output will be an array of numbers [1, 2, 3, 4, 5]

Here’s the complete example:

function sampleFunction() {
let myArray = [1, 2, 3, 4, 5];
return myArray;
}

let result = sampleFunction();
console.log(result);

Output:

[ 1, 2, 3, 4, 5 ]

Here’s another example:

let fName = "It";
let lName = 'sourcecode';
let age = 18;
let address = "USA";

function sampleFunction() {
  let myArray = [fName, lName, age, address];
  return myArray;
}

let result = sampleFunction();
console.log(result); 

Output:

[ 'It', 'sourcecode', 18, 'USA' ]

Solution 2: Use the Array.from() method

You can use Array.from() method to return an array from a function. The Array.from() method creates a new array from an iterable or array-like object.

Here’s an example:

function sampleFunction() {
let myString = '12345';
let myArray = Array.from(myString);
return myArray;
}

let result = sampleFunction();
console.log(result); 

Output:

[ '1', '2', '3', '4', '5' ]

Solution 3: Use the new Array constructor

You can also use the new Array constructor to create and return an array from a function.

Here’s an example:

function sampleFunction() {
let myArray = new Array(1, 2, 3, 4, 5);
return myArray;
}

let result = sampleFunction();
console.log(result);

Output:

[ 1, 2, 3, 4, 5 ]

These are just a few examples of how you can return an array from a function in JavaScript. There are many other ways to do this as well, depending on your specific needs and requirements.

Compare two arrays and return the difference JavaScript

There are several ways to compare two arrays and return the difference in JavaScript.

One way is to use the filter method in combination with the includes method to create a new array that contains only the elements that are present in one array but not the other.

Here’s an example:

let array1 = [10, 20, 30, 40, 50];
let array2 = [30, 40, 50, 60, 70];

let difference = array1.filter(x => !array2.includes(x)).concat(array2.filter(x => !array1.includes(x)));

console.log(difference);

Output:

[ 10, 20, 60, 70 ]

In this example, we have two arrays array1 and array2. We use the filter method on array1 to create a new array that contains only the elements from array1 that are not present in array2.

We do the same for array2, and then use the concat method to combine the two resulting arrays into a single array. The final result is an array that contains the elements that are present in one of the input arrays but not both.

Conclusion

In conclusion, this article discusses various methods for returning arrays from functions in JavaScript.

This article also provides three solutions to return arrays from functions: using a local variable, utilizing the Array.from() method, and employing the new Array constructor.

Additionally, the article demonstrates how to compare two arrays and return their difference using the filter and includes methods.

By understanding this tutorial thoroughly developer can handle arrays and their manipulation within functions.

We are hoping that this article provides you with enough information that helps you understand the JavaScript array return.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment