Categories
JavaScript Answers

How to compare two variables containing strings in JavaScript?

Spread the love

Sometimes, we want to compare two variables containing strings in JavaScript.

In this article, we’ll look at how to compare two variables containing strings in JavaScript.

How to compare two variables containing strings in JavaScript?

To compare two variables containing strings in JavaScript, we can use the === operator.

For instance, we write:

const a = 'foo'
const b = 'foo'
console.log(a === b)

to compare strings a and b with === to check if they’re equal.

Therefore, the console log logs true since they’re the same.

Conclusion

To compare two variables containing strings in JavaScript, we can use the === operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *