Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu February 16, 2023 › #difference #HowTo

Difference between == and === in JavaScript with example

Today I'm going to talk about the difference between == and === in JavaScript.

If you’re a JavaScript programmer, then you know that one of the most important concepts to understand is why == (double equals sign) and === (triple equals sign) are different. It can be confusing at first, but understanding this concept will make your code much more reliable. So, let's take a look at what each one means.

The double equals sign (==) is an operator used for comparison between two values. This operator will attempt to convert both sides of the equation into compatible types before checking if they are equal or not. For example:

<script>
  console.log(2 == '2') // true
</script>

If you run the above example, the output will be "true". Because, the double equals sign (==) converts "2" into a number before comparing them.

On the other hand, the triple equals sign(===) performs strict comparisons without any type conversion. So if you use this operator then both sides need to be exactly identical in order for it to return true. For example :

<script>
  console.log(2 === '2') // false
</script>

If you try to run the above example, the output will be "false". Because, one is an integer and one is a string.

save
listen
AI Answer
Write Your Answer
loading
back
View All