{"id":782,"date":"2022-07-11T08:32:59","date_gmt":"2022-07-11T08:32:59","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=782"},"modified":"2023-04-22T19:39:53","modified_gmt":"2023-04-22T19:39:53","slug":"javascript-comparison-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-comparison-operators\/","title":{"rendered":"Comparison Operators in Javascript"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong>Javascript Comparison Operators<\/strong><\/p>\n\n\n\n<p>As the name says, these operators are used to perform different comparisons, like checking whether something is greater than, equal to, or less than something else, or something like that. At times, we need to have such comparisons in our programs, and whenever we need some comparisons, we are going to make use of the Comparison Operators in Javascript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-center\">Comparison Operators in Javascript<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Comparison Operators in Javascript | Lecture 11 | JavaScript for Beginners\" width=\"696\" height=\"392\" src=\"https:\/\/www.youtube.com\/embed\/SvGfrkGRYHI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>This concept is pretty interesting, and here are some operator symbols that we use as Comparison <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-operators\/\">Operators in Javascript<\/a> &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Operator<\/td><td>Description<\/td><\/tr><tr><td>==<\/td><td>Comparison for Equalness.<\/td><\/tr><tr><td>===<\/td><td>Comparison for strict equals(value and type of the data, both should be equal)<\/td><\/tr><tr><td>! =<\/td><td>Comparison for unequalness.<\/td><\/tr><tr><td>!= =<\/td><td>Comparison for strict unequalness(value or type should be unequal)<\/td><\/tr><tr><td>&gt;<\/td><td>One value is greater than another one<\/td><\/tr><tr><td>&lt;<\/td><td>One value is lesser than the other one<\/td><\/tr><tr><td>&gt;=<\/td><td>One value is greater than or equal to the another one<\/td><\/tr><tr><td>&lt;=<\/td><td><span style=\"font-size: revert; background-color: var(--wp--preset--color--background); color: var(--wp--preset--color--foreground); font-family: var(--wp--preset--font-family--system-font);\">One value is lesser than or equal to the another one <\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As you can see, in the above table, we have got many different comparison operator symbols. Basically, all we are doing here, in comparison, we are checking here, that whether or not something is equal to another thing, or something is greater than another thing, or less than another thing, and so on. These operators are going to return a true or false value.<\/p>\n\n\n\n<p>We are also going to have a look at the program, so as to get the concept properly. Let&#8217;s have a look at the program. You can refer to the code to understand Comparison Operators in <a href=\"https:\/\/gyanipandit.com\/programming\/category\/javascript-tutorial\/\">Javascript<\/a> \u2192<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"827\" height=\"185\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators.jpg\" alt=\"\" class=\"wp-image-783\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators.jpg 827w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-300x67.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-768x172.jpg 768w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"340\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-1.jpg\" alt=\"\" class=\"wp-image-784\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-1.jpg 576w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-1-300x177.jpg 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure>\n<\/div>\n\n\n<p>You can observe the outputs, and study why we have such outputs. Let&#8217;s now talk about them one by one. The first thing, we are checking is whether they are both values equal. Since, both the values are 100 and 100, the output obviously should be true. For the second value, we are checking for strict equals. By strict equal, we mean that the value should also be equal, and the type of the data should also be equal.<\/p>\n\n\n\n<p>Have a look at the below program, where one data is a number 100, and another data is a string 100. but the comparison with == gives a true value, on the other hand, the comparison done with === gives a false value. Have a look &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"83\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-2.jpg\" alt=\"\" class=\"wp-image-785\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-2.jpg 801w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-2-300x31.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-2-768x80.jpg 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/figure>\n<\/div>\n\n\n<p>So, the === evaluates for a strict comparison, where we are checking whether or not the type and value are equal.<\/p>\n\n\n\n<p>If you have a look at the output, it is something like this &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"177\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-3.jpg\" alt=\"\" class=\"wp-image-786\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-3.jpg 575w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-3-300x92.jpg 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure>\n<\/div>\n\n\n<p>Then, we are also talking about the first value being greater than, equal to, or less than the other value, which are some things, which we are already familiar with (generally).<\/p>\n\n\n\n<p>Then, we are also checking for unequalness. Firstly, we are talking about whether the first value is not equal to another value, and then we are also talking about whether the first value is strictly not equal to another value. Have a look at the below program, where we are trying to do the same thing again &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"810\" height=\"70\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-4.jpg\" alt=\"\" class=\"wp-image-787\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-4.jpg 810w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-4-300x26.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-4-768x66.jpg 768w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/figure>\n<\/div>\n\n\n<p>Have a look at the output of the above program. We will try to explain, why are we getting such output.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"170\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-5.jpg\" alt=\"\" class=\"wp-image-789\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-5.jpg 576w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/Comparison-Operators-5-300x89.jpg 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, the first output is obvious, since we know that they are equal, so checking for unequal should give us false. Let&#8217;s talk about the next one. The next one checks if they are strictly unequal. This means that either their values should be unequal, or the type should be unequal. Now, here, the values are equal, but the type is not equal, so we are getting a true output here. So, we get true if either their values are unequal, or the type is unequal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript Comparison Operators As the name says, these operators are used to perform different comparisons, like checking whether something is greater than, equal to, or less than something else, or something like that. At times, we need to have such comparisons in our programs, and whenever we need some comparisons, we are going to make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":{"0":"post-782","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-javascript-tutorial","7":"tag-javascript","8":"tag-javascript-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=782"}],"version-history":[{"count":5,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/782\/revisions"}],"predecessor-version":[{"id":6033,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/782\/revisions\/6033"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}