What are the Operators of JavaScript ?
Let’s say 2 + 4 equals 6, where + is the operator, and 2 and 4 are the operands. Now, I will discuss this type of operator.
At this point, there might be a lot of confusion about what could be done with this. Doubts may arise, but these doubts are the basis of everything for you. So, you need to have a clear understanding of these. There’s nothing you need to remember or memorize. Let it slowly sink into your mind.
In javascript, there are many types of operators
- Arithmetic operator
- Comparison operator
- Logical / Relational operator
- Assignment operator
- Conditional / ternary operator
Their names can give us some idea of what they might be used for. I have discussed each of them in detail below. You will practice them one by one in the crome developer console
- Arithmetic operator :
The arithmetic operators are used for addition, subtraction, multiplication, and division. However, there are a few more beyond these:
( + ) is used to add two operands.
Var A = 10 ;
Var B = 20 ;
A + B
Output 30
( – )To subtract two operands
Var A = 20 ;
Var B = 10 ;
A – B
Output 10
( * ) To multiply two operands
Var A = 10 ;
Var B = 5 ;
A * B
Output 50
( / ) To divide two operands
Var A = 10 ;
Var B = 2 ;
A / B
Output 2
% (Modulo)
To find the remainder, one operator is used. When 13 is divided by 5, the result is 2, but the remainder is 3. This operator is used to find this remainder:
13 % 5
Output 3
++ ( Increment ) :
This will add 1 to your operand. Suppose you have a variable ‘a’ with a value of 10; now, if you write ‘a++’, it will add 1 to its value. This is called the shortcut of ‘a = a + 1’.
– – ( Decrement )
It’s somewhat like the decrement operator, but the value will decrease by 1. Similarly, this is also a shortcut for ‘a = a – 1.
Noticeable: Incremental/decremental operators can be placed before and after your variable in two positions. The difference is that when placed before, the value is first incremented/decremented, then returned. If placed after, the value is first returned, then incremented/decremented. For example, if the increment/decrement operator is placed after
Var A = 10 ;
A++
The line will return a value to the console immediately upon execution. You will notice a value without any increment or decrement. However, upon subsequent observations, you will see that the value has been incremented/decremented.
Var A = 10 ;
A ++
10
A
11
If the increment/decrement operator is placed before
Var A = 10 ;
++A
11
A
11
After displaying this line on the console, you will immediately see a returned value, which has undergone an increment/decrement. Upon subsequent observation, you will notice the same result. This applies even to the decremental operator. This small technique might come in handy for specific tasks, so it’s good to keep it in mind
- Comparison Operators
To compare one value with another for equality or inequality, determining whether they are equal, greater than, or less than each other, the result is calculated as true or false. Especially when comparing two values, if the types of the two values are different, they undergo a special rule where one type is converted to match the other before comparison. However, even in this case, there is a slight difference, which we will examine later.
== (Equal)
is used to check if two values are equal or not. If they are equal, it will return “true”; otherwise, it will return “false”. If they are of different types, then based on certain rules, one of the types will be converted so that both become the same type and then it will be checked whether they are equal or not.
10 == 10
( Is 10 == 10 ? , true )
10 == 20
(Is 10 == 20 ? , false )
The comparison will be based on the value stored in the variable in the same way.
Var A = 10 ;
Var B = 20 ;
Now let’s see if two variables are equal or not
A == B
Output: false
!= (Not Equal )
If two values are not equal, it will show true: 10 != 20.
( Is 10 not equal to 20? True )
And if they are equal, it will show false: 10 != 10
( Is 10 not equal to 10? False )
> ( Greater-than )
To determine if one is greater than the other. If it is greater, it will return true, otherwise it will return false
20 > 20
( Is 20 greater than 20? , false )
< ( less than )
To determine if one is smaller than the other. If it is smaller, it will return true, otherwise it will return false
10 < 20
( Is 10 less than 20? , true )
>= ( Greater Than or Equal )
To determine if one is greater than or equal to the other. If it is greater than or equal, it will return true, otherwise it will return false.
10 >= 10
Is 10 greater than 10? or equal? True
<= ( less than or equal )
To determine if one is smaller than or equal to the other. If it is smaller than or equal, it will return true, otherwise it will return false.
10 <= 5
( is 10 less than or equal to 5? false )
10 <= 10
( Is 10 less than or equal to 10 ? true )
Post Comment