Comparing String In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Ashish
In this tutorial we will show you the solution of comparing string in JavaScript, here we using strict equality operator for compare two different strings with each other then result will displayed on webpage.
This operator will check each other with associated data type and characters case it checks if both are same then it returns true otherwise it returns false.
Step By Step Guide On Comparing String In JavaScript :-
Here we defined div tag with id ‘res’ for appends result in webpage. In script we defined two string variables ‘str1,str2’ with two different strings then we using strict equality operator we comparing str1 with str2.
It returns Boolean value either it true or false depends on the strings if both are same then it will return true otherwise it return false we appends that this result on webpage in detail.
<!DOCTYPE html> <html> <head> <title>STRING COMPARE</title> </head> <body> <div id="res"></div> <script> const str1="Hi"; const str2="Hii"; var r=str1 === str2; console.log(str1 === str2); document.getElementById('res').innerHTML="String1 is "+str1+" and String2 is "+str2+" comparision result is "+r; </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In html we created <div> tag with ID ‘res’ for appends the result on webpage.
- In <script> we defined two string variables namely ‘str1,str2’ with values ‘Hi,Hii’ using ‘const’ keyword.
- Using strict equality operator we comparing two strings and result of Boolean value stored on variable ‘r’.
- Then the result is appends to html div element by ‘innerHTML’ and also displayed on console. So we appended result with div tag will displayed on webpage.
- Both </body>, </html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to compare strings using javascript.
When we executing this program on browser we can see the result of Boolean value ‘false’ displayed on webpage.
For seeing result on console we need to use ‘ctrl+shift+j’ shortcut or right click on empty space select last option ‘inspect’ on browser window then panel will open at right hand side with result. In javascript we had another method also ‘localCompare’ for string comparisons later we will see about that in detail.
I hope this tutorial on comparing string in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.