Home
Node.js
Equals Example
Updated Dec 13, 2023
Dot Net Perls
Equals. In Node.js we can compare things with two equals signs or three equals signs. There is a subtle difference here. But in most cases the two have the same effect.
JavaScript notes. With three equals signs, the types of the two variables must be equal. With two equals signs, a conversion may occur in the runtime.
Example. Here we have a string that stores digit characters (text). And we have a number that directly stores its integer value.
Start The two variables are considered equal with the "==" operator as the string is converted to a number.
Next The two variables are not equal here as they have different types. The inner block is not reached.
Finally This evaluates to true as the two values have unequal types. This operator tests both types and values.
var text = "800"; var number = 800; // Use type conversion with two equals. if (text == number) { console.log("OK"); } // Do not allow type conversion with 3 equals. if (text === number) { console.log("Not reached"); } // Use not equals with no type conversion. if (text !== number) { console.log("OK 2"); }
OK OK 2
In programming, we try to reduce the number of steps needed. The best practice remains as before: use three equals signs when possible. A possible conversion is eliminated.
Summary. With three equals signs, we compare both the type and the value of two variables. With two equals signs, only the values are compared—conversions may occur.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 13, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen