How to Compare Strings in TypeScript

Rana Hasnain Khan Feb 02, 2024
How to Compare Strings in TypeScript

We will introduce how to compare strings in TypeScript. We will also introduce different methods to compare strings with examples in TypeScript.

Compare Strings in TypeScript

There are times in programming when we want to compare two different strings and check whether they are the same or not. For example, if we’re going to compare passwords when a user is trying to log in, It will be a very useful function for us.

There are several methods to achieve this scenario in TypeScript. We will discuss these methods in detail.

Strict Equality Operator in TypeScript

We can use this operator to check whether two strings are equal or not in the transcript.

Syntax:

# typescript
if (pass1 === pass2) {}

When strings are equal, the strict equality operator will return true, and if the strings are not equal, false will be returned.

Code:

# typescript
const pass1 = 'Admin!123';
const pass2 = 'Admin!123';

if (pass1 === pass2) {
console.log('Passwords are equal!');
} else {
console.log('Passwords are not equal!');
}

Output:

compare strings in typescript first example

In this example, we have used the strict equality (===) operator, which has allowed us to check if two TypeScript strings are equal.

The operator returns a Boolean result True if the provided values are equivalent. It’ll return False if the provided values are not equivalent.

Comparisons are case-sensitive when using the strict equality (===) operator to compare strings.

Code:

# typescript
const pass1 ='admin123';
const pass2 ='ADMIN123';

if(pass1=== pass2){
console.log('Passwords are equal');
}else{
console.log('Passwords are NOT equal');
}

Output:

compare strings in typescript second example

Use the tolowercase() method on both strings when you want to make a case insensitive comparison.

Code:

# typescript
const pass1 ='admin123';
const Pass2 ='ADMIN123';

if(pass1.toLowerCase() === Pass2.toLowerCase()){
console.log('Passwords are equal');
}else{
console.log('Passwords are not equal');
}

Output:

compare strings using lowercase method in typescript

We can also check if the two strings are not equivalent. Apply the strict inequality (!==) operator to check it.

Code:

# typescript
const pass1 ='admin123';
const Pass2 ='Admin123';

if(pass1 !== pass2){
console.log('Passwords are equal');
}else{
console.log('Passwords are not equal');
}

Output:

compare strings in typescript third example

There are two possibilities regarding whether values are equal or not. If values are similar, the strict inequality (!==) operator will return false, and if values are not equal, it will return true.

There is a difference between the strict equality (===) operator and the loose equality (===) that it will contemplate two values of unlike types NOT to being equal.

Code:

# typescript
const pass1 = 'admin123';
const pass2 = Admin123;

if (pass1 == pass2) {
console.log('Passwords are equal');
} else {
console.log('Passwords are not equal');
}

Output:

compare strings in typescript fourth example

In this example, we used the loose equality operator to see if the number 100 is equivalent to the string '100'. If we had used Javascript, the result would have been accurate, but an error would appear as we have used TypeScript.

It is better to use the strict equality (===) operator and should be used to compare values of the same type. The strict equality (===) operator contemplates two values of different types to be not the same but in the case of the loose equality(===) operator, the scenario is the opposite.

In simple words, we can say that while using the strict equality operator, two values having different types will never be equal to each other. It is better to use the strict equality (===) operator and should be used to compare values of the same type, and it will not intimidate the values to the same kind while associating them and will return more readable and more straightforward results.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - TypeScript String