Operators & Expressions in JS

Andrew Moussa
2 min readMar 27, 2021
Photo by Tracy Adams on Unsplash

As i started to learn JS i was overwhelmed with how many JavaScript’s expressions and operators i should memorise or at least recognise. In this Artikel i will try to cover most of them including assignment, comparison, arithmetic, bitwise, logical and string.

Assignment operators (=, += , -= , *=)

An assignment operator assigns a value to its left operand based on the value of its right operand

//Assignment operatorsConsole.log( A = B) // A equal BConsole.log( x +=1) // Adds 1Console.log( x -=1) // subtracts 1)Console.log( x *=3) // multiplies 3 with x)

Comparison operators (< , > , == , != )

A comparison operator compares its operands and returns a logical value based on whether the comparison is true

//Comparison operatorsconsole.log(0 > 1) // FALSEconsole.log(0 < 1) // TRUEconsole.log(1 == 1) // TRUEconsole.log(1 != 1) // FALSE

Arithmetic Operators (+, − , ∗, / )

Arithmetic operators are used to perform mathematical operations between numeric operands.

//Arithmetic Operators10 + 5; //returns 15

10 - 5; //returns 5

5 * 10; //returns 50

10 / 2; //returns 2

5 % 2; //returns 1

5++; //returns 6

5--; //returns 4

Logical Operators ($&&, ||, ! $)

//Logical Operatorsconsole.log( 1 || 0) // 1 OR 0console.log(1 && 0) // 1 AND 0console.log(!true)  // FALSEconsole.log(!1)     // FALSEconsole.log(!false) // TRUEconsole.log(!0)     // TRUE

Comma Operator (,)

The comma operator evaluates both of its operands and returns the value of the last operand. Primarily used to add multiple parameters in a for loop

for (var a = 0, b =5; a <= 5; a++, b--)

But also in a return statement. Process before returning using

function myFun() {
var a = 0;
return (a += 1,a);
}

Bitwise Operators (&, |, ^)

Bitwise operators are used to change individual bits in an operand

//Bitwise Operatorconsole.log(5 & 1) //returns 1console.log(5 | 1) // returns 5console.log(5 ^ 1) //returns 4

Expressions

Anything that evaluates to a value is called an expression. Some of the basic expressions and keywords used in JavaScript are mentioned below:

  • this: points to the current object
  • super: calls methods on an object’s parent, for example, call parent’s constructor
  • function: used to define a function
  • function*: used to define a generator function
  • async function: used to define an async function

--

--

Andrew Moussa

Many years experience in Journalism getting hands-on in teamwork and communication