Type Conversion and basic operator and maths
Tags: Operators, JavaScript, Programming, Math

Terms: “unary”, “binary”, “operand”

An operand is what operators are applied to. For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2 . Sometimes, people call these “arguments” instead of “operands”.

there are two operands: the left operand is and the right operand is . Sometimes, people call these “arguments” instead of “operands”. An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:

let y = 1; y = -y;

alert (y); // y is -1 unray negation is applied

An operator is binary if it has two operands (arguments). The same minus exists in binary form as well

let y = 2;

let x = 3; alert(x - y); // 1, binarny minus substracts values

Maths

Addition + ,

, Subtraction - ,

, Multiplication * ,

, Division / ,

, Remainder % ,

, Exponentiation ** .

Remainder

The remainder operator % , despite its appearance, is not related to percent.

The result of a % b is the remainder of the integer division of a by b

alert( 5 % 2 ); // 1, a remainder of 5 divided by 2

alert( 8 % 3 ); // 2, a remainder of 8 divided by 3

Exponentiation

The exponentiation operator a ** b multiplies a by itself b times

alert( 2 ** 3 ); // 8 (2 * 2 * 2, 3 times)

alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2, 4 times)

String concatenation with binary +

Usually, the plus operator + sums numbers.

But, if the binary + is applied to strings, it merges (concatenates) them

let conc = 'hello' + 'world';

alert(conc); // helloworld alert('1' + 2); // "12"

alert(2 + '1'); //"21"

Note that if any of the operands is a string, then the other one is converted to a string too

Here’s a more complex example

alert(3 + 3 + '2'); // "62" not "232"

The first + sums two numbers, so it returns 6 , hen the next + adds the string 2.

The binary + is the only operator that supports strings in such a way

Ex of subtraction and division

alert( 6 - '3' ); // 3, converts '3' to a number

alert( '6' / '2' ); // 3, converts both operands to numbers

Numeric conversion, unary +

The plus + exists in two forms: the binary form that we used above and the unary form.

The unary plus or, in other words, the plus operator + applied to a single value, doesn’t do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.

// No effect on numbers

let x = 2;

alert( +x ); // 2 let y = -6;

alert( +y ); // -6 // Converts non-numbers

alert( +true ); // 1

alert( +"" ); // 0

It does the same thing as Number(...) , but is shorter.

The need to convert strings to numbers arises very often. For example, if we are getting values from HTML form fields, they are usually strings

The binary plus would add them as strings:

let me = '5';

let you = '5'; // both values converted to numbers before the binary plus alert(+me + +you); // 10 // the longer variant

// alert( Number(apples) + Number(oranges) ); // 5

Assignment

Let’s note that an assignment = is also an operator. It is listed in the precedence table with the very low priority of 3

That’s why, when we assign a variable, like x = 2 * 2 + 1 , the calculations are done first and then the = is evaluated, storing the result in x .

let x = 2 * 2 + 1; alert( x ); // 5

Chaining assignment

let a,b,c; a = b = c = 2+2; alert(a); // 4

alert(b); // 4

alert(c); // 4

Chained assignments evaluate from right to left. First, the rightmost expression 2+2 is evaluated and then is assigned to on the left c , b , a .

All the variables share a single value.

c = 2 + 2;

b = c;

a = c;

Once again, for the purposes of readability it’s better to split such code into few line

Increment / decrement

Increasing or decreasing a number by one is among the most common numerical operations!

Increment → ++

let guessMyNumber = 5;

let guessMynumber++; alert(guessMyNumber); // 6

Increment ++ increases a variable by 1

decrement →- -

let guessMyNumber = 5;

let guessMynumber++; alert(guessMyNumber); // 4

Decrement -- decreases a variable by 1

Increment/decrement can only be applied to variables. Trying to use it on a value like 5++ will give an error.

The operators ++ and -- can be placed either before or after a variable.

When the operator goes after the variable, it is in “postfix form”: guessMyNumber++

The “prefix form” is when the operator goes before the variable: ++guessMyNumber

Both of these statements do the same thing: increase guessMyNumber by 1

let guessMyNumber = 2;

let a = ++guessMyNumber; // (this) alert (a); // 3

In the line (this) , the prefix form ++guessMyNumber increments guessMyNumber and returns the new value, 3 . So, the alert shows 3 .

let guessMyNumber = 12;

let a = guessMyNumber++; // (this) alert (a); // 12

In the line (this) , the postfix form guessMyNumber++ also increments guessMyNumber but returns the old value (prior to increment). So, the alert shows 12.

If the result of increment/decrement is not used, there is no difference in which form to use

let guessMyNumber = 0;

guessMyNumber++;

++guessMyNumber; alert (guessMyNumber); // 2

If we’d like to increase value and immediately use the result of the operator, we need the prefix form

let guessMyNumber = 0;

alert( ++guessMyNumber); // 1

If we’d like to increment a value but use its previous value, we need the postfix form

let guessMynumber = 0;

alert (guessMyNumber++); // 0

Increment/decrement among other operators

The operators ++/-- can be used inside expressions as well. Their precedence is higher than most other arithmetical operations.