[JS] – What is the difference between == and === ?

Understanding by examples

0 == false

Result : True. 0 is automatically converted to boolean.

0 === false

Result :  False. Don’t have here automatically conversion and they are of a different types.

1 == “1”

Result :  True. Automatic type conversion for value only.

1 === “1”

Result : False. Don’t have here automatically conversion and they are of a different types.

null == undefined

Result :  True. Automatic type conversion for value only.

null === undefined

Result : False. Don’t have here automatically conversion and they are of a different types.

Conclusion

If you use “==”  is because you want the js use the automatic type conversion before the comparison and if you use “===” is because you don’t want it makes the conversion before the comparison.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s