Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 04 / 31THE LANGUAGE

Conversion and equality.

Understand comparisons by noticing when JavaScript changes a type.

01 / THE BASICS

Start here.

An operator is a symbol that combines or changes values. Plus can add numbers, as in 2 + 3, or join text, as in '2' + '3'. When one operand is text, JavaScript may convert the other to text. That is why '5' + 2 becomes '52'. Subtraction has no text-joining meaning, so '5' - 2 becomes 3 after numeric conversion. This is called coercion: a value is converted because an operation asks for another kind.

A clearer calculation converts an input deliberately. Number('5') gives the number 5, and Number('five') gives NaN, a special number value meaning the conversion failed. You can inspect that case with Number.isNaN. Strict equality, written ===, asks whether two values are equal without the conversions performed by ==. The !== operator means strictly not equal. For ordinary values, 5 === '5' is false because their types differ.

Objects add another distinction. Two separate object literals, such as { name: 'Ada' } and another { name: 'Ada' }, create two identities. Comparing them with === yields false even though their properties look alike. If two names refer to the same object, their strict comparison is true. To compare their contents, choose the properties that define sameness and compare those explicitly. This makes your program's meaning visible rather than hiding it in a conversion rule.

02 / THE IDEA

Build the model.

Operators sometimes convert their inputs. The plus operator joins strings if one side becomes a string; subtraction asks for numbers. Loose equality performs its own conversions, which can make a comparison depend on rules the reader cannot see at a glance.

Prefer === and !== for most comparisons, and convert input deliberately at a boundary. Strict equality compares primitives by value and objects by identity. It also has two notable number cases: NaN is unequal to itself, while positive and negative zero compare equal.

03 / UNDER THE SURFACE

How it works.

Input from a form is usually text, even when the person typed digits. If you add that text to a number, + may concatenate. Decide where text becomes a number and write the conversion there. Number('5') gives 5, while Number('five') gives NaN. After converting uncertain input, Number.isNaN or Number.isFinite can help you reject a result that arithmetic cannot safely use.

Strict equality avoids implicit conversion, but it does not compare object contents. Two object literals create two identities, so {} === {} is false. Two bindings pointing to the same object compare true. For numeric edge cases, NaN === NaN is false; Number.isNaN tests that case. If you need deep value equality, define which properties and order matter instead of assuming === can infer your intent.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
console.log('5' + 2); // '52'
console.log(Number('5') + 2); // 7
console.log({} === {}); // false

First notice. The last line creates two distinct objects with the same shape.

In '5' + 2, the presence of a string leads + to concatenate, producing '52'. Number('5') first creates the number 5, so the next + performs addition and produces 7. In {} === {}, each brace pair creates a separate object, and strict equality compares their identities rather than their visible fields.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const input = '12';
const quantity = Number(input);
console.log(Number.isFinite(quantity) ? quantity + 1 : 'invalid');

The output is 13. Number(input) turns the text into a number, Number.isFinite confirms it is usable, and only then does + add one. If input were 'twelve', the conversion would produce NaN and the result would be 'invalid'.

06 / KEEP THIS

One thing to remember.

Make conversions visible; compare objects by identity unless you write a value comparison.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.