admin管理员组文章数量:1323150
There was an answer to a nested array problem on stack overflow that involved a return statement +a + +b. (no period) What is this? What does it do? It seems to add, but I'm not sure how it does that. How does it differ from a + b? The code is below, and it works:
var array= [1, 2, [3, 4], [], [5]];
var sum = array.toString().split(",").reduce(function(a, b) { return +a + +b;
});
console.log(sum);
There was an answer to a nested array problem on stack overflow that involved a return statement +a + +b. (no period) What is this? What does it do? It seems to add, but I'm not sure how it does that. How does it differ from a + b? The code is below, and it works:
var array= [1, 2, [3, 4], [], [5]];
var sum = array.toString().split(",").reduce(function(a, b) { return +a + +b;
});
console.log(sum);
Share
asked Sep 1, 2018 at 22:32
ACFACF
931 gold badge2 silver badges10 bronze badges
3
-
6
The
+
in front coerces a variable to a number. – CertainPerformance Commented Sep 1, 2018 at 22:32 -
2
"5" + 6 === "56"
whereas+"5" + 6 === 11
– ibrahim mahrir Commented Sep 1, 2018 at 22:38 -
The
+
in front of a variable means the developer doesn't know what is going on but tries to calculate anyway, if possible. – Frederik.L Commented Sep 1, 2018 at 22:43
5 Answers
Reset to default 10The +
before the variable is the unary +
operator. You are probably familiar with the unary -
, e.g. -x
.
Both +x
and -x
convert x
to a number, but -x
also changes its sign. This is not the primary purpose of the existance of the unary +
, but it is a nice sideeffect.
Therefore, the difference between a + b
and +a + +b
would be visible from this example:
x = '9'; // '9'
x = +'9'; // 9
x = '9' + '9'; // '99'
x = +'9' + +'9'; // 18
JavaScript is dynamically typed, but has no built-in operators for casting a value to ensure it is the right type for an operation. Consequently, various tricks are used when it's important that a value is treated as a number, or a string, etc.
In this case, the operator being used is the "unary +" which is the natural counterpart of the "unary -" that would be used to write a negative number: just as "-x" means "0 - x", "+x" means "0 + x". On a number, this has no effect, but other values will be converted to a number.
This is important because the "binary +" operator does different things depending on the type of its operands: if a was a string, a + b would mean "append two strings together" rather than "sum two numbers".
Given an imaginary cast_to_number function, the equation could be more readably written as return cast_to_number(a) + cast_to_number(b)
.
Assuming a
or b
were of type String, adding a +
operator in front of it will coerce it to a number.
This ensures that ’1’ + ‘1’
will be 2
(addition of number
s) and not 11
(concatenation of string
s).
javascript has a unary operator +
and -
, both of them converts the operand to a number type, but the -
operator negate the result.
for example:
> -'20'
-20
> +'500' + +'600'
1100
The +
unary operator is used for explicit coercion in JavaScript.
Even though some developers look down upon it, saying var num = 3 + +c;
is rather ugly especially to people not aware of the coercion at hand, potentially even "error-like" looking piece of code.
The most mon practical use of the +
unary operator is probably to get a number timestamp of the date
var timestamp = +new Date(); // this works!
Perhaps a good piece of memorablia is the -
unary operator, which for some reason our human brain seems much happier to interpret. Basically, if we see var x = -c;
we presume the piler:
- Attempts to do a value to number coercion of the variable
c
- If it can return a number, flip the sign and return the negative number (
-
)
The +
unary operator does the same thing, only without the sign flipping.
本文标签: multidimensional arrayWhat does ab mean in JavaScriptStack Overflow
版权声明:本文标题:multidimensional array - What does +a + +b mean in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742143948a2422705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论