Essential operators to understand Javascript


Published on 14 April 2022


Essential operators to understand Javascript

Explanation of essential cases as operators in Javascript

There are special cases in Javascript that we can have doubts about the execution of operators, equalities, data types. In the following parts we will observe in a particular way some of these peculiar cases.

Comparison operators

Symbol

Example

Result

Explanation

==

"1"==1

true

"1" equals 1 even though "1" is a string and 1 is a number

!=

"2"!=1

true

The type of value, string or number, is not important, it is only valid that 2 is not equal to 1

===

 "1"===1

false

They are not equal as the type of value does not match, a string is not equal to a number

!==

"1"!==1

true

They are not equal because the type of value does not match, a string is not equal to a number

<=,>=

2<=3

true

2 is less than or equal to 3 so the condition is met

<,>

2>3

false

2 can never be greater than 3

Arithmetic operators

12% 5 = 2

The remainder of 12/5

3++ = 4

3+1 = 4

3-- = 2

3-1 = 2

-true

-1

+true

1

2**3

2exp(8)

Assignment Operators

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)

Ternary operator

10 > 3 ? 'yes' : 'no';

if(10 > 3){ 'yes' }else{'no'};

Choosing between the most appropriate variable

In the following cases Javascript chooses between the favorite "a" or "b" variable value:

  • let a = undefined;     
    let b = 1;     
    let favorite = a || b; //Result: 4

 

  • let a = 7;     
    let b = 4;     
    let favorite = a || b; //Result: 7

 

  • let a = 7;     
    let b = null;     
    let favorite = a || b; //Result: 7

Previous Chapter - Next Chapter


Tips on SEO and Online Business

Next Articles

Previous Articles