Returning multiple values from a function using an array

Returning multiple values from a function using an array

Suppose the following getNames() function retrieves the first name and last name from a database in the backend or from the result of a third-party API call and returns them as elements of an array:


Article content

The following shows how to get the return value from the getNames() function:


Article content


Article content

In ES6, you can use the destructuring assignment syntax to unpack values from an array more intuitively, like this:


Article content

In this code, the firstName and lastName variables will take the first and second elements of the return array.

To view or add a comment, sign in

More articles by Thiều Quang Khoa

  • JavaScript Interview Questions (Part 4)

    61 What are the main rules of promise 62 What is callback in callback 63 What is promise chaining 64 What is…

  • JavaScript Interview Questions (Part 3)

    41 What are the differences between cookie, local storage and session storage 42 What is the main difference between…

  • Top 400 JavaScript Interview Questions (Part 2)

    21 What is the Temporal Dead Zone 22 What is an IIFE (Immediately Invoked Function Expression) 23 How do you decode or…

  • Top 400 JavaScript Interview Questions (Part 1)

    No. Questions 1 What are the possible ways to create objects in JavaScript 2 What is a prototype chain 3 What is the…

  • Javascript: isFunctions (Part 2)

    5. isUndefined Hàm này kiểm tra xem giá trị có phải là hay không.

  • Javascript: isFunctions (Part 1)

    1. isNumber Hàm này kiểm tra xem giá trị có thuộc kiểu và không phải là hay không.

  • Javascript Interview Questions

    5 features of javascript What is DOM? Data types in javascript? Difference between let, var, const Difference between…

  • JavaScript Reversed Words

    All Javascript Reserved Keywords The below list cannot be used as variable names, as they are reserved keywords. Most…

  • Using parseInt() and toString() for conversions

    Trong JavaScript, việc chuyển đổi giữa các định dạng số là một tác vụ phổ biến, và có nhiều phương pháp để thực hiện…

  • Javascript: Splice vs Slice

    Trong JavaScript, splice và slice là hai phương thức được sử dụng để làm việc với mảng, nhưng chúng có mục đích và cách…

Insights from the community

Others also viewed

Explore topics