Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the + operator before a variable in Javascript?

Tags:

javascript

I was looking into Raphael JS library but I see this:

Animation.prototype.delay = function (delay) {
    var a = new Animation(this.anim, this.ms);
    a.times = this.times;
    a.del = +delay || 0;
    return a;
};

What is the + operator before the delay variable?

Thanks.

like image 485
totten Avatar asked Aug 29 '12 09:08

totten


2 Answers

It converts a String variable to a Number, if possible: +'21.2' equals Number(21.2). If the conversion fails, it return NaN (that's where || 0 kicks in in your example code)

like image 149
KooiInc Avatar answered Oct 12 '22 02:10

KooiInc


It is a way to make a variable value to number, if the variable has a number. alternative you can use Number function.

like image 25
Yograj Gupta Avatar answered Oct 12 '22 03:10

Yograj Gupta