JavaScript Operators

JavaScript Operators

JavaScript Operators


javascript Operators

We know many operators from school: addition +multiplication *subtraction -, and so on. In this chapter, we will talk about the aspects of operators that are not covered by school arithmetic.

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

There is some terminology, which you should know before going on.

  • An operand means what operators are applied to. For example, in the multiplication of 4 * 2 there are two operands: the left operand is 4 and the right operand is 2.
  • If an operator has a single operand it is unary. For example, theunary negation - opposites the sign of a number
let x = 2;
x = -x;
console.log(x); // -2,  here was applied unary negation
If an operator has two operands it is binary. The same minus also exists in binary form:
let x = 2,  y = 3;
console.log(y - x); // 1, binary minus subtracts values

There are two different operators in the examples above, which share the same symbol: the negation operator, a unary operator, and the subtraction operator.

String concatenation, binary +

These special features of JavaScript operators are beyond school arithmetics.

Generally the plus operator + sums numbers, but if the binary + is applied to strings, merging them:

let str = "my" + "string";

console.log(str); // string

If one of the operands is a string, the other one is transformed to a string as well:

console.log( '2' + 3 ); // "23"

console.log( 3 + '2' ); // "32"

Both the first operand or the second can be strings. The rule is simple anyway: if one of the operands is a string, the other one is converted into a string too.

Please, note that operations run from left to right and if there are two numbers which followed by a string, they will be added before being converted to a string:

console.log(3 + 2 + '1' ); // "51" and not "321"

String concatenation is one of the special features of the binary plus + . Other arithmetic operators work only with numbers converting their operands to numbers (subtraction, division).

console.log( 2 - '1' ); // 1

console.log( '6' / '2' ); // 3

Numeric conversion, unary +

The plus + operator exists in two forms: the binary form and the unary form.

The plus operator + applied to a single value and it does not do anything to numbers. But when the operand is not a number, the unary + will convert it into a number:

// this doesn’t effect numbers 

let x = 1;

console.log( +x ); // 1

let y = -2;

console.log( +y ); // -2

// converts not numbers

console.log( +true ); // 1

console.log( +"" );   // 0

Assignment =

We use the assignment operator (=) to assign a value to the JavaScript variable, it always returns a value. It is obvious for most of them like addition + or multiplication *.

let x = 3 * 2 + 1;

console.log( x ); // 7

Remainder %

The remainder operator % does not have anything related to percents, the result of a % b is the remainder of the integer division of a by b.

console.log( 7 % 2 ); // 1 is a remainder of 7 divided by 3

console.log( 11 % 3 ); //  2 is a remainder of 11 divided by 3

console.log( 12 % 3 ); // 0 is a remainder of 12 divided by 4

Exponentiation **

The exponentiation operator ** was recently added to the language. For a number b, the result of a ** b is a multiplied by itself b times:

console.log( 3 ** 2 ); // 9  (3 * 3)

console.log( 3 ** 3 ); // 27  (3 * 3 * 3)

console.log( 3 ** 4 ); // 81 (3 * 3 * 3 * 3)The operator works also for non-integer numbers:

The operator works also for non-integer numbers:

console.log( 9 ** (1/2) ); // 3 (power of 1/2 is the same as a square root)

console.log( 27 ** (1/3) ); // 3 (power of 1/3 is the same as a cubic root)

Increment/decrement

Increasing or decreasing a number by 1 is one of the most common numerical operations. There are special operators for it:

  • Increment ++ increases a variable by 1
let counter = 10;
counter++;  // it's a counter = counter + 1
// it will be shorter
console.log( counter ); // 11
Decrement -- decreases a variable by 1
let counter = 10;
counter--;  //it's a counter = counter - 1
// it is shorter
console.log( counter ); // 9

Increment or decrement can only be applied to variables, so If we try to use it on a value like 6++ it will give an error.

We can place the operators ++ and -- before or after a variable.

  • When the operator is after the variable, it is in “postfix form” : counter++.
  • When the operator is before the variable, it is in “prefix form” : ++counter.

These two statements do the same thing: increase counter by 1.

let counter = 1;

let a = ++counter; 

console.log(a); // 2\

Increment/decrement among other operators

The operators ++/-- can also be used inside expressions. Their priority is higher than most other arithmetical operations.

Let’s compare these two examples:

let counter = 2;

console.log( 2 * ++counter ); // 6

and

let counter = 2;

console.log( 2 * counter++ ); // 4, because counter++ returns the "old" value

Technically they are okay, but this notation usually makes code less readable. One line does multiple things, what is not good.

During reading code, a fast “vertical” eye-scan can easily miss something like counter++ and it won’t be obvious that the variable increased. That’s why we advise a style of “one line – one action”:

let counter = 1;

console.log( 2 * counter );//2

counter++;

console.log( counter );//2

Reactions

Post a Comment

0 Comments

close