December 3, 2026 - 08:49
JavaScript Lesson 05 — Why Does JavaScript Join Values Instead of Adding Them? Image
JavaScript Tutorials

JavaScript Lesson 05 — Why Does JavaScript Join Values Instead of Adding Them?

0 Comments

You add 120 and 35 but get 12035 instead of 155. Arithmetic has not failed: one value is probably text. This is particularly common with values read from form controls. Let us isolate the behavior in a small calculation.

Inspect the actual values

JAVASCRIPT
const unitPrice = 120;
const quantity = 2;
const delivery = 35;
const total = unitPrice * quantity + delivery;
console.log(total);
console.log('120' + 35);
console.log(Number('120') + 35);
console.log(Number(''));
console.log(Number('abc'));

The total is 275: 120 multiplied by 2, plus 35. The expression '120' + 35 produces the string 12035. Here the addition operator performs concatenation because it encounters a string. Explicit conversion with Number produces 155 in the next line.

Conversion is not a replacement for validation. Number('') produces zero, even though an empty field and a deliberate zero may mean different things. First consider whether an input is empty, then whether it represents an acceptable number.

Number('abc') produces NaN. This is not a usable ordinary quantity. In a form example we can check a conversion with Number.isFinite. Even a successful conversion does not prove the value satisfies a business rule.

Follow the order of operations

JAVASCRIPT
console.log(10 + 5 * 2);
console.log((10 + 5) * 2);
console.log(10 / 4);
console.log(10 % 4);
console.log(Number('12,5'));
console.log(Number('12.5'));

The first two results are 20 and 30. Multiplication precedes addition; parentheses request the other order. Division gives 2.5 and the remainder operation gives 2. The percent sign here means remainder, not “calculate a percentage.”

The comma decimal string produces NaN; the dot decimal string produces 12.5. Number is not a locale-aware parser for every way users write numbers. Accepting comma decimals in a form requires a deliberate input rule.

Number and parseInt ask different questions

JAVASCRIPT
console.log(Number('12px'));
console.log(parseInt('12px', 10));
console.log(parseInt('12.8', 10));
console.log(Number('12.8'));

The results are NaN, 12, 12 and 12.8. Number requires a numeric conversion of the string as a whole, while parseInt can read an initial integer portion. A successful parseInt therefore does not prove an entire form field is valid numeric text.

Discarding decimals is not a universal validation rule either. Decide whether 12.8 is allowed before choosing the operation.

Check emptiness separately

JAVASCRIPT
const raw = ' 12 ';
const cleaned = raw.trim();
const value = Number(cleaned);
console.log(cleaned === '');
console.log(Number.isFinite(value));
console.log(value);

The output is false, true and 12. Trim removes surrounding whitespace. If raw contains only spaces, cleaned is empty even though Number would convert it to zero. The separate empty check preserves the distinction.

Number.isFinite does not first coerce a string: Number.isFinite('12') is false, while the converted number 12 passes. Conversion and validation are separate operations.

Decimal arithmetic has representation limits

JAVASCRIPT
console.log(0.1 + 0.2);
console.log((0.1 + 0.2).toFixed(2));

The first result is normally 0.30000000000000004. The second is the string 0.30. Number uses binary floating-point representation. Formatting with toFixed does not turn it into exact financial arithmetic, and the formatted result is text.

Real monetary calculations may use integer minor units or an appropriate decimal design. Those choices belong to the application's rules rather than a blanket formatting patch.

Remainder with negative values

JAVASCRIPT
console.log(5 % 2);
console.log(-5 % 2);

The outputs are 1 and -1. JavaScript's remainder is not an always-positive modulo operation. Testing a negative input reveals the difference hidden by a simple positive example.

Use empty, alphabetic, decimal and boundary inputs as well as comfortable whole numbers. Explain why each is accepted instead of checking only that one arithmetic expression prints an expected value.

Separate calculation from presentation

'Toplam: ' + total deliberately joins a label and a value for display. Concatenation itself is useful; accidentally using it where arithmetic was intended is the problem. Keeping the numeric calculation separate from its label makes this easier to see.

This is a programming exercise, not a payment calculation system. Real financial code must also handle decimal precision and the currency's rounding rules.

Set the quantity to 3 and check for 395. Then quote the unit-price value: multiplication can convert that string numerically, so the calculation may still appear to work. Inspect the type as well as the final result.

References: Reference 1 · Reference 2

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment