JavaScript Compare Strings Examples

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

This tutorial will show you two methods ( Equality Operator, localecompare) of javascript to compare two strings in javascript. Let’s see the method below:

1. Method First – javascript compare strings Equality Operator Method

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

 ==

Example:-

    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 – javascript Compare Strings 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.

Example:-

    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

AuthorAdmin

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, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL 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 *