10

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.

3 Answers 3

17

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)

1
  • Wouldn't it had been better if they have made the fail = 0 there?
    – Z2VvZ3Vp
    Jul 19, 2017 at 18:58
7

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

0

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!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.