Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does slash equal in javascript do?

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?

like image 488
Mark Schultheiss Avatar asked Dec 13 '11 20:12

Mark Schultheiss


People also ask

What does slash do in JS?

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.

What does double slash do in JavaScript?

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.

How do you use forward slash in JavaScript?

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.

What is double slash in coding?

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.


1 Answers

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.
like image 76
Andrew Barber Avatar answered Oct 02 '22 15:10

Andrew Barber