DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 5 Of JavaScript

Hey everyone ,I am back with my fifth post on JavaScript. In the last post I have mentioned about data types in JavaScript ,in this post we are going to study about operators and conditionals used in JavaScript. So let's get started :)

There are 7 types of operators used in JavaScript ->

1. Arithmatic Operators ->

Arithmetic operators perform arithmetic on numbers.

Image description

Addition, subtraction and multiplication operators are the basic operators , so I'll not talk much about these.

Exponentiation Operator

This operator is used to find a power b where a and b are two numbers.

Image description
Math.pow(a,b) also does same thing.

Division Operator

It will give quotient when a number num1 is divided by num2.

Image description

Modulus Operator

It will give remainder when a number num1 is divided by num2.

Image description

Increment Operator

It is of two types -:

  • Pre Increment Operator ->It will increase the value before the expression is evaluated.

Image description
That means, if you use ++i within an expression, the value of i is increased first, and then the updated value is used in the expression.

  • Post Increment Operator ->It will increase the value after the expression is evaluated.

Image description
That means, if you use i++ within an expression, the current value of i is used first in the expression, and then i is incremented.

And same works for Decrement Operator.

2. Comparison Operators ->

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Image description

== and === Operator ->

Image description
So here you can clearly see the difference between two operators
For === the value and data type of variable must be same.
And same holds for != and !== operators.

Image description

3. Logical Operators ->

Logical operators are used to determine the logic between variables or values.

Image description
These are basically used in conditions along with comparison operators.

Image description

4. Bitwise Operators ->

Bit operators work on 32 bits numbers.
Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.

Image description
Below is the working of these bitwise operators.

Image description
Here we have considered 4-bit number representation but JS represents number in 32-bits so ~5 will be equal to -10.

5. Assignment Operators ->

Assignment operators assign values to JavaScript variables.

Image description

Apart from these we have shift assignment operators too.

Image description

Bitwise Assignment Operators ->
Image description
Logical Assignment Operators ->

Image description

The ??= Operator->
The Nullish coalescing assignment operator is used between two values.
If the first value is undefined or null, the second value is assigned.

Image description

6. Ternary Operator ->

Ternary Operators can be used in place of if conditions.
Here is an example-:

Image description
So here if x===5 then "hi" is printed otherwise "bye".
We can do ternary nesting just like we do if-else nesting.

Image description
So the statement basically means ->

Image description

7. Type Operator ->

You can use the typeof operator to find the data type of a JavaScript variable.

Image description
You can see that ->
The data type of a variable that has not been assigned a value is undefined.

This is it for operators. Now let's move to conditionals.

Conditional Statements ->

In JavaScript we have the following conditional statements:

  • if-else conditional -> We use if-else block to check whether the condition is true or false ,if the condition is true if block is executed otherwise else block will work.

Image description

  • else if condtional -> This is used to check multiple conditions, if one condition is not true then the other will be checked and the process goes on till it reach else block.

Image description

  • switch statement -> The switch statement is used to perform different actions based on different conditions.

Image description
Here in this code ->
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If there is no match, the default code block is executed.

So this is it for this post we are going to see more about Loops in next post . Till then stay connected :)

Top comments (0)