Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - '...' meaning

Tags:

javascript

I am looking at a open source javascript application, specifically an extension for firefox.

I am seeing this syntax in multiple places that I do not know what it means if anyone can shed some light on this.

such as..

 return (...args)

or...

 console.info(message, ...args.slice(1));

any idea what this '...' does? Is it like getting the third argument in or what? Third argument back? Its hard to try and debug this without being able to understand it.

like image 377
tneduts Avatar asked Feb 09 '15 23:02

tneduts


People also ask

What is JavaScript in simple words?

JavaScript is the Programming Language for the Web. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data.

What is JavaScript and why it is used?

Javascript is used by programmers across the world to create dynamic and interactive web content like applications and browsers. JavaScript is so popular that it's the most used programming language in the world, used as a client-side programming language by 97.0% of all websites.

What does $() mean in JavaScript?

Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .

What is JavaScript used for example?

The primary use of JavaScript is to build web-based applications. Some of what JavaScript can do for the web includes: adding interactive behavior to web pages like zooming in and out or playing audio/video. creating web and mobile apps, the most popular examples consist of the likes of Netflix and Uber.


2 Answers

It's an Ecmascript 6 "rest" parameter. When used as a parameter or argument, it lets you receive or pass an array as individual arguments.

http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html

like image 31
recursive Avatar answered Oct 02 '22 21:10

recursive


It will unpack an array (args) into a formal argument list. Amongst other things this allows the members of a rest parameter to be passed as a set of formal arguments to another function.

Here's an example:

var stats = function(...numbers) {
    for (var i=0, total = 0, len=numbers.length; i<len; i++) {
        total += numbers[i]; 
    } 
    return {
        average: total / arguments.length,
        max: Math.max(numbers); //spread array into formal params
    } 
} 

stats(5, 6, 8, 5); //{average: 6, max: 8}

Hope this help you understand "..."!

like image 119
Surfyst Avatar answered Oct 02 '22 19:10

Surfyst