Day 5 Of JavaScript
Tags: javascript, beginners, programming, tutorial
Published: 2024-04-26 15:23:30

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q3095vfut54jipkffyn.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsxtuw08ztscwn6aiyun.png)
Math.pow(a,b) also does same thing.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7juoin7t8ojzsgpw9uqz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01w57wbksfk6d77cro52.png)

## Increment Operator
It is of two types -:

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy8y1y0ch7toyqjqxjdv.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po8byspdefgwjf92f15w.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxc3pkw8tnnjy6ocwqyw.png)

**`==` and `===` Operator ->**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn70qhs6weixn10b9280.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpv6674n3zrpxsbl5nmi.png)

## **3. Logical Operators ->**
Logical operators are used to determine the logic between variables or values.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehy5zaqjrcfhpczmykkn.png)
These are basically used in conditions along with comparison operators.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3qpwke6lp36uu825302.png)

## **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4l4kta6yylw1hmpf1nb.png)
Below is the working of these bitwise operators.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh3ye73d9p035izlyjy3.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y190ls8yzswtu85shtl.png)

Apart from these we have shift assignment operators too.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbpfr1fgwftztepp3x29.png)

Bitwise Assignment Operators ->
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl5ndygu610jsrk7soix.png)
Logical Assignment Operators ->

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdcxhzdcvee6mh75mij5.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96ko9tsgdvvuqhnvgody.png)

## **6. Ternary Operator ->**

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp09efa4ipl82yflkgv2.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsbqmq25056mztzejfzj.png)
So the statement basically means ->

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzud75xgzgb19rbcvcp6.png)

## **7. Type Operator ->**

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy30ft601hqvl3grbyyu.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1evqgnr816v8h063pres.png)

- `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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5iltvy1tgreczek2tc96.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq0quanz0ed3bqwh8o7x.png)
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 :)