JavaScript Compare Strings – Examples

JavaScript Compare Strings – Examples

In this tutorial, you will learn how you can compare two strings in javascript using the equality operator and localeCompare method with the definition of this method, syntax, parameters, and several examples.

Compare String JavaScript

In this tutorial, we will explain two methods of javascript to compare two strings in javascript. Let’s see the method below:

1. Method First – Equality Operator

You can use the equality operator of javascript to compare two strings in javascript

Definition:- The javascript equality operator, which is used to compare two values on both sides and it will return the result as true or false.

Syntax

 ==

Ex:-

    var stringFirst = "javascript world";
    var stringSecond = "javascript world";
    var res = '';

    if(stringFirst == stringSecond)
    {
      res = 'strings are equal';
    }else
    {
      res = 'strings are not equal';
    }

    document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript string compare</title>
</head>
<body>
  <script type = "text/javascript">
    
    var stringFirst = "javascript world";
    var stringSecond = "javascript world";
    var res = '';

    if(stringFirst == stringSecond)
    {
      res = 'strings are equal';
    }else
    {
      res = 'strings are not equal';
    }

    document.write( "Output :- " + res ); 

  </script>  
</body>
</html>

Result of the above code is:

Output :- strings are equal

Remember some points of JavaScript Equality Operator

  • If this method returns true, strings are equal.
  • If this method returns false, strings are not equal

2. Second Method – localeCompare() Method

You can use the localeCompare() Method of javascript to compare two strings in javascript.

Definition:-The javascript localeCompare() method is used to on a string object for comparing two strings.

Syntax:-

string.localeCompare(compareString);

Note:- this method will return 0, -1 or 1. This method does case-sensitive comparing.

Ex:-

    var stringFirst = "javascript world";
    var stringSecond = "javascript world";
  
    var res = stringFirst.localeCompare(stringSecond);
    document.write( "Output :- " + res + "<br>"); 

    var stringThird = "javascript world";
    var stringFourth = "javascript";
  
    var res1 = stringThird.localeCompare(stringFourth);
    document.write( "Output :- " + res1 ); 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript string compare | javascript localeCompare() Method</title>
</head>
<body>
  <script type = "text/javascript">
    
    var stringFirst = "javascript world";
    var stringSecond = "javascript world";
  
    var res = stringFirst.localeCompare(stringSecond);
    document.write( "Output :- " + res + "<br>"); 

    var stringThird = "javascript world";
    var stringFourth = "javascript";
  
    var res1 = stringThird.localeCompare(stringFourth);
    document.write( "Output :- " + res1 ); 

  </script>  
</body>
</html>

Result of the above code is:

Output :- 0
Output :- 1

Remember some points of localeCompare() method

  • If this method returns 0, the string is sorted before the compare string
  • Returns -1 by this method, the string is sorted after the compare string
  • If this method returns 1, two strings are not equal

JavaScript More string Methods, You may like

  1. javaScript Replace() |javaScript String Replace All
  2. Remove First Character From String Javascript
  3. javaScript String Contains | String includes() Method
  4. Remove Last Character From String Javascript
  5. JavaScript String Methods List | JavaScript Tutorial
  6. JavaScript Concatenate Strings | String concat() Method

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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