What is the Difference Between =, ==, and === in JavaScript




What is the Difference Between =, ==, and === in JavaScript

What is = in JavaScript? 

Equivalent to (=) is a task administrator, which sets the variable on the left of the = to the worth of the articulation that is to its right side. This administrator doles out lvalue to rvalue. 

For instance, Writing a=10 is fine. In the event that we compose 10=10, 'a' = 10 or 'a' = 'a', it will bring about a reference blunder. 

What is == in JavaScript? 

Twofold equivalents (==) is an examination administrator, which changes the operands having a similar kind before correlation. 

Thus, when you contrast string and a number, JavaScript changes any string over to a number. An unfilled string is consistently converts to nothing. A string with no numeric worth is converts to NaN (Not a Number), which returns bogus. 

What is === in JavaScript? 

=== (Triple equivalents) is a severe fairness examination administrator in JavaScript, which returns bogus for the qualities which are not of a comparative kind. This administrator performs type projecting for uniformity. On the off chance that we contrast 2 and "2" utilizing ===, it's anything but a bogus worth. 

Why use = in JavaScript? 

Here are the significant employments of = in JavaScript: 

= JavaScript administrator relegates a worth to one side operand relies upon the worth of operand accessible on the right side. The principal operand ought to be a variable. 

The fundamental task administrator is =, that doles out the worth of one operand to another. That is, a = b doles out the worth of b to a. 

Why use == in JavaScript? 

Here are the significant employments of == in JavaScript: 

The == administrator is a correspondence administrator. It checks if its two operands are something very similar by changing articulation from one information type to other people. You can utilize == administrator to think about the personality of two operands despite the fact that, they are not of a comparable sort. 

How === Works Exactly? 

  • Severe equity === watches that two qualities are something very similar or not. 
  • Worth are not certainly changed over to some other worth before correlation. 
  • Assuming the variable qualities are of various kinds, the qualities are considered as inconsistent. 
  • On the off chance that the variable are of a similar sort, are not numeric, and have a similar worth, they are considered as equivalent. 
  • In conclusion, If both variable qualities are numbers, they are viewed as equivalent if both are not NaN (Not a Number) and are a similar worth. 

Example of = 

In the underneath program, there are two factors "a" and "b". We are adding and printing their qualities utilizing a third factor, "c". The amount of the worth of variable "a" and "b" is 7. Thusly, the yield is 7.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Operators</h2>

 <p>a = 2, b = 5, calculate c = a + b, and display c:</p> 

<p id="demonstration"></p>

<script>
var a = 2;
var b = 5;
var c= a + b;
document.getElementById("demonstration").innerHTML = c;
</script>

</body>
</html>

Example of ==

In the below program, we have declared one variable "a" having value 10. Lastly, the statement a == 20 returns false as the value of a is 10.

<!DOCTYPE html>
<html>
<body>

<p id="demonstration"></p>

<script>
  var a = 10;
  document.getElementById("demonstration").innerHTML = (a == 20);
</script>

</body>
</html>

Example of ===

In the below program, the value of variable x is 10. It is compared to 10 written in double-quotes, which is considered as a string, and therefore, the values are not strictly the same. The output of the program is false.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>

  var x = 10;
  document.getElementById("demo").innerHTML = (x === "10");

</script>

</body>
</html>

KEY DIFFERENCES:

  • = is used for assigning values to a variable, == is used for comparing two variables, but it ignores the datatype of variable whereas === is used for comparing two variables, but this operator also checks datatype and compares two values.
  • = is called as assignment operator, == is called as comparison operator whereas It is also called as comparison operator.
  • = does not return true or false, == Return true only if the two operands are equal while === returns true only if both values and data types are the same for the two variables.


Author Biography.

Editorial Team
Editorial Team

Content Writer

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

417e9598e1b8bdba442a9bac4874f919.jpg

JavaScript Mobile Frameworks worth considering ...

When it comes to startups entering the business arena, they have very tough competition. This is ...

d7c8b3433a607583dc5bb1bf47f7041f.png

How to Generate a PDF with JavaScript

An extremely regular use case today is that of enabling your clients to download information from...

d432ce51653c4558fc49e6f6ba3ca0c0.jpg

How to Play Audio After Few Seconds or Delay in...

In this instructional exercise, I will demonstrate to you how effortlessly you can play sound fol...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Hey, I am Suraj - a full-time blogger and a social media expert currently working on the Growth H...

Suraj Kumar

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta
CFT

Our Client Says

WhatsApp Chat with Our Support Team