Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow function with Square Brackets?

I am working on some code, and I have stumbled across something I am unfamiliar with.

export const doSomething = () => [ someFunction(), bind(stuff, stuff, stuff) ]; 

I have never seen an arrow function with a square bracket like that, has anyone else? If so what is the meaning of how it works?

like image 573
chris Avatar asked Nov 04 '19 21:11

chris


People also ask

Do arrow functions need brackets?

There are two ways to declare an arrow function: Without curly braces {} . With this syntax, the arrow function has an implicit return. For example, the below arrow function returns 42, even though there's no return .

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What does => mean in programming?

What It Is. This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {...} .

What does square brackets mean in C?

A postfix expression (operand followed by the operator) followed by an expression in square brackets [ ] is a subscripted designation of the array element . The definition of the array subscript operator [ ] is that if a is an array and i is an integer then a[i] =*(a+i) .

What is an arrow function in JavaScript?

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

What do square brackets mean in math?

Square brackets, as in [ π] = 3, are sometimes used to denote the floor function, which rounds a real number down to the next integer. Respectively, some authors use outwards pointing square brackets to denote the ceiling function, as in ]π [ = 4.

What are arguments in Arrow functions?

Arrow functions do not have their own arguments object. Thus, in this example, arguments is simply a reference to the arguments of the enclosing scope: In most cases, using rest parameters is a good alternative to using an arguments object.

How to add a line break to an arrow function?

An arrow function cannot contain a line break between its parameters and its arrow. However, this can be amended by putting the line break after the arrow or using parentheses/braces as seen below to ensure that the code stays pretty and fluffy. You can also put line breaks between arguments.


Video Answer


2 Answers

This code means that your function doSomething returns an array when

[0] element - the result of execution of function someFunction() and

[1] element - the result of execution of function bind(stuff, stuff, stuff).

This is a shortcut for:

export const doSomething = () => {
    return [ someFunction(), bind(stuff, stuff, stuff) ]
}; 

But be careful if you want to make a shortcut for returning objects. You have to wrap objects in parentheses (), like this:

export const doSomething = () => ({ name: 'John' }).

like image 188
Andrii Golubenko Avatar answered Oct 19 '22 04:10

Andrii Golubenko


It's just returning an array.

You might use it with a destructuring assign e.g.

const [someResult, boundStuff] = doSomething()

Or just like any old function e.g.

const something = doSomething()
like image 4
Dylan Avatar answered Oct 19 '22 02:10

Dylan