QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads JavaScript Function Return Multiple Values

JavaScript Function Return Multiple Values

This post is a part 3 of ECMAScript 6 Complete Tutorial post series.

There were many different ways purposed by JavaScript developers for returning multiple values in an function. But ECMAScript 6 has come up with an easy way to do it.

Here is an code example of how to return multiple values using ES6:

function function_name()
{
    return [1, 6, 7, 4, 8, 0]; //here we are storing variables in an array and returning the array
}

var q, w, e, r, t, y;

//Here we are using ES6's array destructuring feature to assign the returned values to variables.
//Here we are ignoring 2 and 4 array indexes
[q, w, , r, , y] = function_name();

alert(y);//y is 0

ES6 provides a array like syntax to assign multiple variables to values of array indexes. It also lets you ignore some array indexes.

Feb 13, 2015Narayan Prusty
JavaScript "const" KeywordDefault Function Arguments Values in JavaScript
Comments: 7
  1. Juliet
    6 years ago

    Great to see…wonderful work…

    ReplyCancel
  2. Md. Hasib Hasan Tarafder
    7 years ago

    Hi,

    I’m just wondering that it works in Firefox, but not in Chrome whereas i use traceur.js.

    Why it is not working in Chrome? Did u check it in chrome ever?

    ReplyCancel
    • Narayan Prusty
      7 years ago

      I have tested all the examples in es6fiddle.com

      Browsers still don’t support ES6 properly.

      ReplyCancel
      • Md. Hasib Hasan Tarafder
        7 years ago

        thanks a lot

        ReplyCancel
    • Digender
      5 years ago

      You can use chrome canary it supports most of the ES6 features.

      ReplyCancel
  3. Md. Hasib Hasan Tarafder
    7 years ago

    Hi Narayan,

    I run your above example. But, it gives the error Uncaught ReferenceError: Invalid left-hand side in assignment

    Can you please check and let me clear the things

    ReplyCancel
    • Narayan Prusty
      7 years ago

      Just tried it. It works well for me.

      ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

7 years ago 7 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • JavaScript “…” Operator
  • Default Function Arguments Values in JavaScript
  • JavaScript “Map” Object
  • JavaScript “Set” Object
  • JavaScript “for of” Loop Tutorial
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license