Reputation: 2817
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.
Upvotes: 11
Views: 5916
Reputation: 1
It is a Unary Operator. It converts/parses numbers from a string
, boolean
or even null
values.
It can:
Parse a number from a string, so +"23" returns 23
Parse +True/+False to 1 or 0 respectively.
even +null would return 0.
You can, of course, perform Math.*
functions on these above-mentioned returns.
eg.
let str = "25.5";
Math.ceil(+str) // would return 26
I hope this helps!
Upvotes: 0
Reputation: 122906
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)
Upvotes: 18
Reputation: 9869
It is a way to make a variable value to number, if the variable has a number. alternative you can use Number
function.
Upvotes: 7