What does a slash do in javascript?

I recently looked at Javascript code in a jQuery plugin and came across this line of code:

duration /= 2; 

Another code shows that the duration variable is a numerical value.

Can someone explain what exactly this is doing with a slash?

+8
javascript jquery
source share
4 answers

It divides your duration variable by 2.

 duration = 4; duration /= 2; // duration now is 2 
+14
source share

This is equivalent to this:

 duration = duration / 2; 

You can do the same with the +, -, and * operators, as well as 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. 
+9
source share

These are two operations:

  • Department
  • Appointment

You probably saw this with the addition and subtraction before: += or -=

+2
source share

Rewriting this for clarity:

You can do the same with the +, -, and * operators, as well as with many others, as a shortcut:

 var duration = 2; // declaring the variable "duration" and assigning it the value of 2 duration += 2; // duration = duration + 2 // now duration is 4 duration *= 2; // duration = duration * 2 // now duration is 8 duration -=4; // duration = duration - 4 //now duration is 4 again 
0
source share

All Articles