2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Arithmetic Operation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2018-11-04 07:05:18 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								JavaScript provides five arithmetic operators: `+` , `-` , `*` , `/`  and `%` . The operators are for addition, subtraction, multiplication, division and remainder, respectively.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Addition
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`a + b`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2 + 3          // returns 5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    true + 2       // interprets true as 1 and returns 3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    false + 5      // interprets false as 0 and returns 5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    true + "bar"   // concatenates the boolean value and returns "truebar"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    5 + "foo"      // concatenates the string and the number and returns "5foo"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "foo" + "bar"  // concatenates the strings and returns "foobar"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-03 08:49:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_Note:_ There is a handy [increment(++) ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment ) operator that is a great shortcut when you're adding numbers by 1.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Subtraction
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`a - b`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2 - 3      // returns -1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3 - 2      // returns 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    false - 5  // interprets false as 0 and returns -5
							 
						 
					
						
							
								
									
										
										
										
											2018-10-15 22:00:47 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    true - 3   // interprets true as 1 and returns -2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    5 - "foo"  // returns NaN (Not a Number)
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-03 08:49:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_Note:_ There is a handy [decrement(--) ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement ) operator that is a great shortcut when you're subtracting numbers by 1. 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Multiplication
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`a * b`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2 * 3                // returns 6
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3 * -2               // returns -6
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    false * 5            // interprets false as 0 and returns 0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    true * 3             // interprets true as 1 and returns 3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    5 * "foo"            // returns NaN (Not a Number)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Infinity * 0         // returns NaN
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Infinity * Infinity  // returns Infinity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Division
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`a / b`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3 / 2                // returns 1.5
							 
						 
					
						
							
								
									
										
										
										
											2018-10-21 04:02:11 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    3.0 / 2.0            // returns 1.5
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    3 / 0                // returns Infinity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3.0 / 0.0            // returns Infinity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    -3 / 0               // returns -Infinity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    false / 5            // interprets false as 0 and returns 0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    true / 2             // interprets true a 1 and returns 0.5
							 
						 
					
						
							
								
									
										
										
										
											2018-10-15 22:00:47 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    5 / "foo"            // returns NaN (Not a Number)
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    Infinity / Infinity  // returns NaN
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Remainder
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-03 08:49:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Also referred to as the "modulus" or "modulo" operator.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`a % b`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3 % 2          // returns 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    true % 5       // interprets true as 1 and returns 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    false % 4      // interprets false as 0 and returns 0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3 % "bar"      // returns NaN
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Increment
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 13:34:01 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`a++, ++a or a+=`  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Postfix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    x = 3;  // declare a variable 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    y = x++;        // y = 4, x = 3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Prefix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var a = 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    b = ++a; // a = 3, b = 3
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 13:34:01 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Advanced
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var a = 4;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var b = 5;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    a += b; //a = 9, b = 5
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Decrement
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Syntax**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 13:34:01 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`a--, --a or a-=`  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Postfix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    x = 3;  // declare a variable 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    y = x--;        // y = 3, x = 3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Prefix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var a = 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    b = --a; // a = 1, b = 1
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 13:34:01 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Advanced
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var a = 9;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var b = 4;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    a -= b; //a = 5, b = 4
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-03 08:49:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								_!Important!_ You **cannot**  perform any sort of operations on `Infinity` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Source: The amazing [MDN ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators )