Strict Equality (==) and Loose Equality (===) in Typescript
Introduction
Typescript is a strongly-typed programming language that is developed and maintained by Microsoft. It is built on top of JavaScript and provides additional features such as static typing, interfaces, and classes. One of the fundamental concepts in Typescript (and JavaScript) is equality comparison. There are two ways to compare the equality of two values: strict equality (==) and loose equality (===). In this article, we’ll look at what these two operators do and how they differ.
Strict Equality (==)
The strict equality operator (==) compares two values for equality but without type conversion. It returns true only if the values match in both type and value. If the values differ in type, they are considered unequal. For example:
“`typescript
const num = 4;
console.log(num == ‘4’); // true
console.log(num == 5); // false
“`
In the first line, the variable `num` has a value of `4`, which is a number. In the second line, we compare it to the string `’4’`, and the operator returns true. This is because the string is coerced into a number before comparison. In the third line, we compare `num` to the number `5`, and the operator returns false because the values are different.
This behavior can be unexpected and lead to subtle bugs in your code. That’s why it’s recommended to use the strict equality operator (===) instead.
Loose Equality (===)
The loose equality operator (===) also compares two values for equality, but with type conversion. It returns true if the values match in both type and value. If the values differ in type, the operator tries to convert one or both values to a common type before comparison. For example:
“`typescript
const num = 4;
console.log(num === ‘4’); // false
console.log(num === 5); // false
“`
In the first line, the operator returns false because the types are different. The second line also returns false because the values are different.
FAQs
Q: Which operator should I use for equality comparison in Typescript?
A: It’s recommended to use the strict equality operator (===) to avoid unexpected behavior and bugs in your code.
Q: Why does the strict equality operator (==) perform type coercion?
A: The JavaScript language specification defines this behavior to make comparisons more flexible and convenient. However, it can lead to unexpected behavior and is not recommended for use in Typescript.
Q: How can I compare two objects for equality in Typescript?
A: The equality operator (== or ===) compares primitive values (such as numbers or strings). To compare objects, you need to define your own comparison function that checks each property for equality.
“`typescript
interface Point {
x: number;
y: number;
}
function pointsEqual(p1: Point, p2: Point): boolean {
return p1.x === p2.x && p1.y === p2.y;
}
const point1 = { x: 1, y: 2 };
const point2 = { x: 1, y: 2 };
console.log(pointsEqual(point1, point2)); // true
“`
Conclusion
In summary, strict equality (===) and loose equality (==) are two ways to compare the equality of two values in Typescript (and JavaScript). The strict equality operator is recommended for use to avoid unexpected behavior and bugs in your code. When comparing objects, you need to define your own comparison function that checks each property for equality.