What does = +_ mean in JavaScript

What does = +_ mean in JavaScript

I was wondering what the = +_ operator means in JavaScript. It looks like it does assignments.
hexbin.radius = function(_) {
if (!arguments.length)
return r;
r = +_;
dx = r * 2 * Math.sin(Math.PI / 3);
dy = r * 1.5;
return hexbin;


Solution 1:

r = +_;
  • + tries to cast whatever _ is to a number.
  • _ is only a variable name (not an operator), it could be a, foo etc.



cast “1” to pure number 1.

var _ = "1";
var r = +_;

r is now 1, not "1".

Moreover, according to the MDN page on Arithmetic Operators:

The unary plus operator precedes its operand and evaluates to its
operand but attempts to converts it into a number, if it isn’t
. […] It can convert string representations of integers and
floats, as well as the non-string values true, false, and null.
Integers in both decimal and hexadecimal ("0x"-prefixed) formats are
supported. Negative numbers are supported (though not for hex). If it
cannot parse a particular value, it will evaluate to NaN.

It is also noted that

unary plus is the fastest and preferred way of converting something into a number

Solution 2:

It is not an assignment operator.

  • _ is just a parameter passed to the function.

    hexbin.radius = function(_) {
                    //       ^ It is passed here
        // ...
  • On the next line r = +_; + infront casts that variable (_) to a number or integer value and assigns it to variable r


Related:  Npm postinstall only on development

Solution 3:

=+ are actually two operators = is assignment and + and _ is variable name.


i = + 5;
j = + i;
i = + _;

My following codes will help you to show use of =+ to convert a string into int.

y = +'5'
x = y +5

outputs 10

use: So here y is int 5 because of =+

y = '5'
x = y +5

outputs 55

Where as _ is a variable.

_ = + '5'
x = _ + 5

outputs 10

It would be interesting to know you could also achieve same thing with ~ (if string is int string (float will be round of to int))

y = ~~'5'  // notice used two time ~
x = y  + 5

also outputs 10

~ is bitwise NOT : Inverts the bits of its operand. I did twice for no change in magnitude.

Solution 4:

It’s not =+. In JavaScript, + means change it into number.

+'32' returns 32.

+'a' returns NaN.

So you may use isNaN() to check if it can be changed into number.

Solution 5:

It’s a sneaky one.

Related:  How do you keep multiple collapses open in Bootstrap 3?

The important thing to understand is that the underscore character here is actually a variable name, not an operator.

The plus sign in front of that is getting the positive numerical value of underscore — ie effectively casting the underscore variable to be an int. You could achieve the same effect with parseInt(), but the plus sign casting is likely used here because it’s more concise.

And that just leaves the equals sign as just a standard variable assignment.

It’s probably not deliberately written to confuse, as an experienced Javascript programmer will generally recognise underscore as a variable. But if you don’t know that it is definitely very confusing. I certainly wouldn’t write it like that; I’m not a fan of short meaningless variable names at the best of times — If you want short variable names in JS code to save space, use a minifier; don’t write it with short variables to start with.

Solution 6:

= +_ will cast _ into a number.

Related:  AngularJS $http object not showing all headers from response


var _ = "1",
   r = +_;
console.log(typeof r)

would output number.