Operators

·

4 min read

Operators are the symbols which are used to perform operations on operands.

let a = 99
let b = 1  
console.log(a + b )

Here variables a and b are operands. the addition sign (+) is an arithmetic operator and (=) equals to sign is the assignment operator and the result of the following code is the result.

There are multiple types of operators in javascript:

Arithmetic operators:

Arithmetic operators are used to perform arithmetic operations on numbers

Table from w3schools.com

Operator

Description

Example 

+

Addition

a+b 

-

Subtraction

a-b

Multiplication

ab

**

Exponentiation (ES2016)

a raised to b 

/

Division

 a/b

%

Modulus (Division Remainder)

a%b

++

Increment

a++

--

Decrement

b--

There is a difference between the division operator and the modulo operator. division operator returns the quotient in float format but the modulo operator only returns the remainder and not the quotient.

The increment operator (a++) increases the value of a number and the decrement operator (b—) decreases the value of a number. Prefixing the operators (++a or --b ) will change the value directly and print it but Postfixing them (a++ or b--) will print the original value first and then increment or decrement.

Assignment operators :

Assignment operators are a combination of Arithmetic Operators and assignment operators. Equals to (=) sign is a base of all assignment operators. It simply assigns values to variables.

Table from w3schools.com

Operator

Example

Same As

=

x = y

x = y

+=

x += y

x = x + y

-=

x -= y

x = x - y

=

x = y

x = x y

/=

x /= y

x = x / y

%=

x %= y

x = x % y

=

x = y

x = x * y

Comparison operators

comparison operators are used to compare values inside of different data types and return the result in boolean values (true or false).

Table from w3schools.com

let a= 10 and b=“10”

Operator

Description

Example 

Result

==

equal to

 a == b

True

===

equal value and equal type

 a === b

False 

!=

not equal

 a != b

False

!==

not equal value or not equal type

 a !== b

True 

>

greater than

 a > b

False 

<

less than

 a < b

False 

>=

greater than or equal to

 a >= b

True 

<=

less than or equal to

 a <= b

True 

?

ternary operator

I don’t know 

About this 

notice equal to(==) and strictly equal to (===) have different results the reason this happened because the strictly equal to (===) operator also compares the data type of variables whereas equal to operators only compares the value (it automatically detects the value stored inside of a string is a number or not) .same works for strictly not equal to (!==) operators and not equal to operators(!=).

Logical operators

logical operators are used to compare boolean values it is based on a mathematics topic called logic and the results are returned on basis of the truth table.

you can learn about truth tables here https://medium.com/i-math/intro-to-truth-tables-boolean-algebra-73b331dd9b94

Table from w3schools.com

let a= 10 and b=11

Operator

Description

Example 

Result 

&&

logical and

a < b && a != b

True 

||

logical or

a < b && a == b

True 

!

logical not

!false

True 

Expression:

a fragment of code that produces a value is called an expression. every value written directly inside code (“Tony Stark”, undefined, 55 ) is an expression. so basically you can write any value inside of code with proper syntax and won't create any error.