Navigate back to the homepage

How to check if a string is equal to another string in javascript?

Infinitbility
JavaScript
May 27th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show how do you check if a string is equal to another string in javascript, here I will use the == and === operator to compare strings and check whether both are equal or not.

  • The == in JavaScript is used for comparing two variables, but it ignores the data type of variable.

  • The === is used for comparing two variables, but this operator also checks datatype and compares two values.

Let’s start today’s tutorial How to check if a string is equal to another string in javascript?

Here, we will take the example of both operators and see how we can use them to compare one string to another string.

We are going to do,

  1. Create a sample string variable
  2. Use the == operator to compare string
  3. Use the === operator to compare string
  4. Console comparison output
1// Create sample string variable
2const str1 = "infintbility";
3const str2 = "1";
4
5// Use the `==` operator to compare string
6// Console comparison output
7console.log(str1 == "infintbility"); // true
8console.log(str1 == "invalid-string"); // false
9
10console.log(str2 == "1"); // true
11console.log(str2 == 1); // true
12
13// Use the `===` operator to compare string
14// Console comparison output
15console.log(str1 === "infintbility"); // true
16console.log(str1 === "invalid-string"); // false
17
18console.log(str2 === "1"); // true
19console.log(str2 === 1); // false

Run the above program, you can easily understand it after reading the above example.

let’s check the output.

javascript, check if a string is equal to another string example
javascript, check if a string is equal to another string example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is hexadecimal in javascript?

Example of check if a string is hexadecimal in javascript with step by step guide.

May 27th, 2022 · 1 min read

How to check if a string is a valid email in javascript?

Example of check if a string is a valid email string in javascript with step by step guide.

May 26th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]