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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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