What does "+ !!" mean do in javascript?

Consider the following codes

var value = 0;
for (var i=0; i < arguments.length; i++) {
    value += +!!arguments[i];
}

What is doing here +!!? Is this one good JavaScript programming style?

+4
source share
3 answers

This is not one operator, it is three: +and then !twice.

What this does is apply !to arguments[i], which leads to true values falseor falsehoods before true, and then applies !to create false=> trueand vice versa, and then applies unary +to convert the result to a number ( true=> 1, false=> 0).

- , false. : 0, "", NaN, null, undefined , , false. - .

, arguments value.

JavaScript?

!!, - true, - false - . + - .

+4

!!arguments[i] - , , arguments[i] .

,

console.log(!!{});
// true

? . ,

Truthy, !Truthy false, !false, true.

Falsy, !Falsy true, !true, false.


+ +!!arguments[i] Number , ( !!arguments[i] ).

JavaScript, true , 1 0 false.

console.log(+true);
// 1
console.log(+false);
// 0
+6

Often !!used to convert a variable to a boolean type and +to a number. In this example, it is first converted to boolean, and then for the number in this case is 1 or 0. The variable valuecontains the number of truthy parameters passed to the function.

0
source

Source: https://habr.com/ru/post/1624560/


All Articles