Arithmetics

In JavaScript, you have the usual arithmetics operators:

1 + 2;  // add
9 / 3;  // divide
1 * 5;  // multiply
9 - 5;  // subtract
10 % 4; // modulus (division remainder)
  • The / operator yields a floating-point result.
console.log(9 / 3);
console.log(1 / 2);
  • The % operator works with both integer and non-integer numbers.
console.log(42 % 10);
console.log(1.1 % 0.3);
  • There is ** operator (similar to Python) that raises to a power.
console.log(2 ** 10);
console.log(2 ** 0.5);
  • If an operand is NaN, so is the result
console.log(2 + NaN);

Combine operator and assignment

Similar to Java, C++ and many other programming languages, you can write:

let counter = 1;
counter += 10;
console.log(counter);

Increment and decrement operators:

Similar to Java, C++ and many other programming languages, you can write:

let counter = 1;
console.log(counter++);
console.log(counter);
console.log(++counter);
console.log(--counter);

Type Conversions

Type Coercion is another term for type conversion. The former is more commonly used by the JavaScript community.

What do you think is the output?

console.log("3" - 2);
Explanation

Here, the non-number argument is converted to number.

What do you think is the output?

console.log("3" + 2);
Explanation

Here, + concatenates strings: the non-string argument is converted to string.

Don't mix types!

JavaScript does not say no when you mix up types in expressions. No matter how complex the expression is and what types it involves, JavaScript will evaluate it. The outcome will be surprising and unexpected in many cases. So, never mix types!

I recommend reading freeCodeCamp's article: JavaScript type coercion explained