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 😊.

Leave a Comment