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.
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.
Math.pow(a,b) also does same thing.
Division Operator
It will give quotient when a number num1 is divided by num2.
Modulus Operator
It will give remainder when a number num1 is divided by num2.
Increment Operator
It is of two types -:
- Pre Increment Operator ->It will increase the value before the expression is evaluated.
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.
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.
==
and ===
Operator ->
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.
3. Logical Operators ->
Logical operators are used to determine the logic between variables or values.
These are basically used in conditions along with comparison operators.
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.
Below is the working of these bitwise operators.
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.
Apart from these we have shift assignment operators too.
Bitwise Assignment Operators ->
Logical Assignment Operators ->
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.
6. Ternary Operator ->
Ternary Operators can be used in place of if
conditions.
Here is an example-:
So here if x===5 then "hi" is printed otherwise "bye".
We can do ternary nesting just like we do if-else nesting.
So the statement basically means ->
7. Type Operator ->
You can use the typeof operator to find the data type of a JavaScript variable.
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 useif-else
block to check whether the condition is true or false ,if the condition is trueif
block is executed otherwiseelse
block will work.
-
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 reachelse
block.
-
switch
statement -> Theswitch
statement is used to perform different actions based on different conditions.
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)