February 28, 2013

What does =+ mean in JavaScript

Question by dk89

I was wondering what the =+ operator means in JavaScript. It looks like it does assignments.

Example:

hexbin.radius = function(_) {
   if (!arguments.length)
       return r;
   r = +_;
   dx = r * 2 * Math.sin(Math.PI / 3);
   dy = r * 1.5;
   return hexbin;
};

Answer by mpm

r = +_;
  • + tries to cast whatever _ is to a number.

Answer by Starx

It is not an assignment operator:

  • _ is just a parameter passed to the function
  • + infront as +_ casts that variable _ to a number or integer value

DO NOT CONFUSE IT WITH += operator

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!