Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
I asked another question here and received a great answer as follows:
$(document).on("keydown", function (e) { if (e.which === 8 && !$(e.target).is("input, textarea") || $(e.target).is('[readonly]')) { e.preventDefault(); } });
Notice the three equal signs ===
in the if-statement. I have always thought you only needed two equal signs ==
for a javascript/jQuery if-statement. Is there any reason for the three?
UPDATE
Sorry for the duplicate question - I searched but didn't find any good questions. I guess I was using the wrong search terms.
When using triple equals === in JavaScript, we are testing for strict equality. This means both the type and the value we are comparing have to be the same.
Equal Sign with 3 Lines The equal sign with three lines means that something is identical or similar to something but not necessarily equal. Thus, a triple equals sign means equivalent. The equivalent is not the same as 'equals'.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.
Triple equal sign in javascript means equality without type coercion.
For example:
1=="1" // true, automatic type coersion 1==="1" // false, not the same type.
Three equal signs indicates both the value and type are equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With