SoftwareSideHustles
Concepts

== vs === vs is vs equals(): Equality in JavaScript, Python and Java Explained

The difference between value equality and identity, why JavaScript has == and ===, why Python's is works for small numbers but not large ones, why Java strings must be compared with equals(), and how to compare objects correctly.

By Software Side Hustles Editorial Team · Published · Updated · 5 min read

Quick answer: there are two questions you can ask about two values: are they equal? (same value) and are they the same object? (identity). JavaScript: use === for almost everything. Python: use == for values and is only for None (and other singletons). Java: use .equals() for objects including String, and == for primitives.

Equality versus identity

python
a = [1, 2, 3]
b = [1, 2, 3]
c = a
a == b # True — same contents
a is b # False — two different list objects
a is c # True — two names for one object

Every language makes this distinction; they just spell it differently.

QuestionJavaScriptPythonJavaC#
Same value?=== (primitives)==.equals()== for strings, .Equals()
Same object?=== (objects)is== (objects)ReferenceEquals()
Loose, with type coercion==———

JavaScript: == versus ===

=== (strict equality) compares without converting types. == (loose equality) converts the operands first, following rules few people remember:

javascript
0 == ""; // true
"0" == false; // true
[] == false; // true
"1" == 1; // true
null == undefined; // true
null == 0; // false
0 === ""; // false
"1" === 1; // false

Use === and !== everywhere. The one idiom some teams allow is x == null, which is true for both null and undefined; the clearer modern alternative is x ?? fallback.

Objects compare by reference

javascript
{ a: 1 } === { a: 1 }; // false — different objects
[1, 2] === [1, 2]; // false

There is no built-in deep equality operator. Compare the fields you care about, use a library function (Node's util.isDeepStrictEqual, lodash isEqual), or in tests use toEqual rather than toBe.

NaN and -0

javascript
NaN === NaN; // false — NaN is not equal to anything, itself included
Number.isNaN(x); // the correct check
Object.is(NaN, NaN); // true
Object.is(0, -0); // false, although 0 === -0 is true

Python: == versus is

== calls __eq__ and compares values. is checks whether two names refer to the same object.

The trap is that is appears to work for numbers and strings in quick experiments:

python
a = int("5"); b = int("5")
a is b # True — CPython caches small integers (-5 to 256)
a = int("257"); b = int("257")
a == b # True
a is b # False — two separate int objects

String literals are often interned too, so "hello" is "hello" may be True — but strings built at runtime usually are not. Results depend on the implementation and context, which is exactly why you must not rely on them. Recent Python versions emit SyntaxWarning: "is" with 'int' literal. Did you mean "=="? when you write x is 5.

Use is for singletons, where identity is the meaning:

python
if value is None: ...
if result is NotImplemented: ...

x is None is preferred over x == None because a class can override __eq__ to return anything; identity cannot be overridden.

Java: == versus .equals()

For primitives (int, double, char, boolean), == compares values. For objects, == compares references:

java
String s = "hello";
String t = "hello";
String u = new String("hello");
s == t; // true — both literals refer to the same interned string
s == u; // false — u is a separate object
s.equals(u); // true — same characters

The literal case "working" is what makes this bug survive testing; strings read from input, built with + at runtime or returned from methods are separate objects. Always use .equals() (or Objects.equals(a, b) when either may be null).

The Integer cache trap

java
Integer a = 127, b = 127;
a == b; // true — values -128..127 are cached
Integer c = 128, d = 128;
c == d; // false — different Integer objects
c.equals(d); // true

Code comparing boxed Integer or Long IDs with == works in development, where IDs are small, and fails in production once they exceed 127.

Implementing equals and hashCode

If you override equals, you must override hashCode so that equal objects have equal hash codes. Otherwise HashSet and HashMap treat equal objects as different, and duplicates appear. Java records generate both from their fields. In Python, defining __eq__ sets __hash__ to None (making instances unhashable) unless you also define __hash__; @dataclass(frozen=True) handles it for you.

Comparing floating-point numbers

0.1 + 0.2 == 0.3 is false in all of these languages because of binary floating-point representation. Compare with a tolerance instead — math.isclose(a, b) in Python, or Math.abs(a - b) < epsilon — as explained in rounding and floating point.

Equality in tests

FrameworkSame objectSame value / structure
Jest / VitesttoBetoEqual, toStrictEqual
pytestassert a is bassert a == b
JUnitassertSameassertEquals
AssertJisSameAsisEqualTo, usingRecursiveComparison()

Using toBe on objects is a common reason for "expected [1, 2] but received [1, 2]".

Frequently asked questions

Should I ever use == in JavaScript?

Rarely. x == null to catch both null and undefined is the only widely accepted use, and many style guides forbid even that.

Why does is work for small integers in Python?

CPython pre-creates integers from -5 to 256 and reuses them. It is an implementation detail and may differ across implementations and versions.

Why does == work for strings in my Java test?

Because both strings are literals in the same program and are interned. Strings created at runtime are different objects.

How do I compare two objects by their contents?

Python: implement __eq__ (or use a dataclass). Java: implement equals and hashCode (or use a record). JavaScript: compare fields, or use a deep-equality helper.

Where this skill pays off

"== versus is in Python" is a real exercise in our preference labelling lab — two plausible explanations where one subtly overstates when is is safe. Ranking answers like these, with a clear justification, is paid AI training work; see how preference ranking works.

Cookies on this site

We use essential cookies to run the questionnaire, practice labs and sign-in. With your permission we'd also like to use analytics to understand which pages help people, and advertising cookies to measure our Google Ads. Rejecting never limits any feature. Cookie policy