I was recently reviewing some Javascript code in a jQuery plug-in and came across this line of code:
duration /= 2;
It appears from other code that the duration variable is a numeric value.
Can someone explain exactly what that does with the slash equal?
A slash. A slash symbol '/' is not a special character, but in JavaScript it is used to open and close the regexp: /... pattern.../ , so we should escape it too.
The double slash is a comment. Triple slash doesn't mean anything special, but it might be a comment that was added right before a division. Show activity on this post. Short answer: Those are just comments.
As the forward slash (/) is special character in regular expressions, it has to be escaped with a backward slash (\). Also, to replace all the forward slashes on the string, the global modifier (g) is used. It will replace all the forward slashes in the given string.
The double slash tells the compiler to ignore everything that follows, until the end of the line. Multiline comments are started by using a forward slash followed by an asterisk (/*). This “slash-star” comment mark tells the compiler to ignore everything that follows until it finds a “star-slash” (*/) comment mark.
It's the equivalent of this:
duration = duration / 2;
You can do the same thing with the +, - and * operators, along with many others, as a shortcut:
var duration = 2;
duration += 2; // now is 4
duration *= 2; // now is 8
duration -=4; // now is 4 again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With