1) ‘ == ’operator
- This operator is known as “equal to”, used to compare the value of a variable against the value of other variables or directly some other value, this equality determination also depends upon the nature of variable i.e. whether its integer, float, etc.
- Equality operator does the type conversion if both operands under comparison are not of the same type.
- Syntax: “ x == y “
- There are certain conditions which are true or false, shall be observed– ‘1’ == 1 // evaluates to true.
The reason behind this is that comparison operators don’t do type check, they just do value comparison, so here the internal type conversion from string to integer occurs and then value comes to be true.
- 0 == null // evaluates to false
- 0 == undefined // evaluates to false
- Null == undefined //evaluates to false
- Let us see few example codes for the same now, the snippet outputs can be cross-checked at the browser’s console only.
Hence the above examples have shown that while doing object comparison if the references are not the same then it will result in false results.
2) ‘ === ’ operator
- Till yet we saw equals to the operator and now we shall incorporate the ‘equals to and equals type’ operator, where the type can also be checked.
- Type checks are sometimes essential together with equality, as the business logic sometimes demands it.
- Syntax– X === Y
- Lets directly see some snippets and will explain those accordingly.
Hence the type is evaluated here.
3) ‘ != ’ operator
- This operator is called ‘not equals’ operator, if two operands being evaluated are not equal then this gives value true.
- Syntax – X != Y
4) ‘ !== ‘ operator
- This operator is used to check not equals and not type equals, i.e. the value, as well as type, doesn’t match, if any of the two conditions are also not true then also this evaluates to true.
- Syntax – X !== Y
- The output against multiple cases has been mentioned in the following snippets.
5) Greater Than ‘>’
- While implementing business logics it can be checked whether any expression generated a value greater than other expressions, if so then the value would evaluate to true else false.’
- Syntax – X > Y
- Let’s see a use case now and analyze the output.
6) Less than operator ‘<’
- If in any business logic the expression carries the operand value at the left-hand side of less than the operator to be less than the value at the right-hand side then this logic returns a true result.
- Syntax – X < Y
- Let’s see a few example snippets as mentioned below, note than the boundary condition doesn’t incorporate the equals condition.
7) Greater than and equals to ‘ >= ‘
- This operator includes the boundary condition along with the greater than condition. Based on the required business logics this condition can be incorporated.
- Syntax – X >= Y
8) Less than or equal to ‘ <= ‘
- This operator includes the boundary condition along with the less than condition. Based on the required business logics this condition can be incorporated.
- Syntax – X <= Y
- Hence, we saw various use cases where the comparisons have been drawn and a variety of outputs have been seen, certain conditions are truthy and false based on the test operand data or the evaluation expression.
- The type checking is also an important concern.
- The null and undefined values when being checked against the operands are special conditions that would appear while implementing the business logic.
- So null checks shall be taken care of appropriately such that unexpected conditions do not appear
- Also, there are frameworks like Jasmine for debugging or either the same can be approached from the browser’s console.
You can also go through our other suggested articles to learn more–
4.5 (2,383 ratings)