11

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

18

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it had been better if they have made the fail = 0 there?
7

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

Comments

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!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.