Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ...!! syntax in ES6?

I was reading through this: https://github.com/pburtchaell/redux-promise-middleware/blob/master/src/index.js

I know that ... is being used as Object spread. I know that !! is used to convert anything into a boolean with the same truthiness.

However knowing this what do they mean when they're put together like ...!!? I have trouble understanding the last line here:

{
  ...resolveAction,
  ...isAction(rejected) ? rejected : {
    ...!!rejected && { payload: rejected }
}
  1. ...resolveAction just spreads the keys of resolveAction.
  2. ...isAction(rejected) ? will check if rejected resolves an action and then will spread it. (Not sure about this one either)
  3. add rejected to object if true
  4. {...!!rejected && { payload: rejected } ????????????????

How is ...!! even valid syntax? There are two options:

  1. If it spreads the object first, then !! would be applied to all the spread keys

  2. If !! is applied first it's a boolean value and it can't be spread.

So does it make no sense whatsoever, or am I missing something because given that code, I assume it's trying to spread a boolean value.

like image 785
m0meni Avatar asked Jan 24 '16 04:01

m0meni


People also ask

What is ECMAScript syntax?

ECMAScript syntax is relaxed to enable it to serve as an easy-to-use scripting language. For example, a variable is not required to have its type declared nor are types associated with properties, and defined functions are not required to have their declarations appear textually before calls to them.

What is ec6 syntax?

JavaScript ES6 (also known as ECMAScript 2015 or ECMAScript 6) is the newer version of JavaScript that was introduced in 2015. ECMAScript is the standard that JavaScript programming language uses. ECMAScript provides the specification on how JavaScript programming language should work.

What is ES5 syntax?

ES5 lets you define object methods with a syntax that looks like getting or setting a property.

What is the syntax of JavaScript value?

JavaScript Values The JavaScript syntax defines two types of values: Fixed values and variable values. Fixed values are called literals. Variable values are called variables.


2 Answers

Okay so after downloading the npm module and going through the transpiled code I found the line:

return dispatch(isThunk(rejected) ? rejected.bind(null, resolveAction) : _extends({}, resolveAction, isAction(rejected) ? rejected : _extends({}, !!rejected && { payload: rejected })));

Of which the relevant part is here:

_extends({}, !!rejected && { payload: rejected })

Basically if !!rejected is true then it'll spread the payload into the object. If it's not _extends({}, false) just returns {}.

The key to this working is that ... has less precedence than any other operator in the entire line. With that in mind you can begin to make sense of it.

like image 122
m0meni Avatar answered Oct 25 '22 22:10

m0meni


The relevant question is What is "x && foo()"?

The weird syntax

{...!!rejected && { payload: rejected }}

is parsed as

{ ... ((!!rejected) && { payload: rejected }) }

Indeed the !! does cast rejected to a boolean, then it is evaluated to the object when truthy. A better way to write this would be

{ ...(rejected ? {payload: rejected} : null) }

And of course the whole inner object literal is superfluous. It could just be

{
  ...resolveAction,
  ...(isAction(rejected) ? rejected : (rejected ? {payload: rejected} : null))
}

(omit parentheses as you see fit)

like image 41
Bergi Avatar answered Oct 25 '22 22:10

Bergi