Loading Now
×

More JavaScript Operator 

More JavaScript Operator 

== , != , < , > , <= , >=  Outside of these, there are a few more operators that are most used. For instance, === and !==. Pay attention, here there are three of them. They basically work like the basic equal == or not equal != operators. However, the real difference is that we’ve mentioned earlier that when comparing, the types of the two operands are sometimes converted to match each other if they are different types. In such cases, this part is skipped in the case of the triple equal, whereas with double equal, if the types are different, it changes the type to see if they are equal. Since types are changed under special rules here, developers might face problems with their usage initially. That’s why triple equal is more popular among everyone. With triple equal, your code will compare the values exactly as you provide them. There won’t be any type of conversion

Some example 

10 == ‘10’

Is string 10 equal to number 10? Since one is a string and the other is a number, in this case, one of them will be converted to the same type as the other, true.

10 == ‘10’ true 

“However, if three operators are given, one change will occur.

10 === ‘10’

Is ‘Number 10’ equal to ‘String 10’? Since triple equals is being used here, there won’t be any type conversion, so ‘Number 10’ will remain ‘Number 10’ and ‘String 10’ will remain ‘String 10’,  false.

10 === ‘10’ false

Usually, everyone uses these, and JavaScript is somewhat unique, so it’s good to remember or understand them. You can try using these with variables if you want.

Logical operators can be of three types.

The “&&” (AND) operator typically sits between two or more expressions. If those expressions are all true, then the whole expression evaluates to true. If any one of those expressions is false, then the whole expression evaluates to false. And if both are false, then also the whole expression evaluates to false.

True && true 

Output true 

True && false 

Output false 

(10 < 20 ) && (20 < 30) 

Both conditions are true so, it will return true.

( 10 < 20 ) && ( 10 > 20 ) 

(( is 10 less than 20 ?) true ) and ( is 10 greater than 20? ) false )

It will return a false 

|| ( Or ) 

The “||” operator typically sits between two or more expressions. If any one expression evaluates to true, the whole expression becomes true. Even if all expressions are evaluated to be true, the whole expression remains true. However, if all expressions are evaluated as false, the entire expression becomes false. This operator can be found above the Enter key on your keyboard.

True || true 

( true or true, true) 

True || false 

( true or false, true )

false || false 

( false or false? false )

‘!’ (not) operator is to reverse the result. If an expression is true, it makes it false, and if it’s false, it makes it true.

!true 

(not (true)? false ) 

!false = true 

!true = false 

!( 10 > 20 , false) true 

The assignment operator

= simple assignment operator:  We have already used this operator. It is used to assign a value to a variable.

Var A = 10 ;

+= addition assignment operator: This sentence translates to: “Adding one’s own value to itself and then assigning it back to oneself is the same thing.

A += 10 full form of this is a = a + 10 

Var A = 10 

A += 10 ; 

Moreover, if you look at the value of  A 

Var A = 10 ;

A += 10 ;

20 

20 

-= Subtraction and assignment operator: This is also a shortcut, much like the previous one. Subtracting one’s own value from oneself and then assigning it back to oneself is the same thing.

Var A = 20 ;

A -= 10 

10 

*= Multiplication and assignment operator: This is also a shortcut. Multiplying one’s own value by itself and then assigning it to oneself at the same time.

A *= 5 full form A = A * 5 

Var A = 5 

A *= 5 

25 

25

 “/=” is the divide and assignment operator; it’s also a shortcut. It divides a value by itself and then assigns the result back to itself.

A /= full form A = A / 2 

Var A = 10 ;

A /= 2 ;

%=  modulo assignment operator : This is another shortcut. Assigning one’s own value and then assigning it back to oneself simultaneously

A %= 5 full form A = A % 5 ;

Var A = 13 ;

Conditional ternary operator : ( .. ? .. : .. ) : Here’s another shortcut. Here the special (?:) characters are used. If the condition is true, then it’s this value, otherwise it’s that value.

Var A = 10 ;

Var B = 20 ;

Now we  want to take another variable and assign a value inside it with a condition

Var c = A > B ? 100 : 200 ;

C will be assigned the value 100 if it’s greater than B else 200. Now, as A is not greater than B, the second expression will be evaluated and assigned to C. Upon checking the value of C,

Var A = 10 ;

Var B = 20 ;

Var C = A > B ? 100 : 200

200

Post Comment