Skip to content

Commit 07395e5

Browse files
tmchowJosh-Cena
andauthored
Fix '1' + 2n BigInt example on Addition page (mdn#43812)
* Fix '1' + 2n BigInt example on Addition page The 'Addition using BigInts' example marked "1" + 2n as throwing a TypeError, but the + operator checks for a string operand before the BigInt/Number mix check, so "1" + 2n performs string concatenation and returns "12". Remove the incorrect line and add a short clarifying section with the real behavior. Fixes mdn#43807 * Update index.md --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 2b6ef41 commit 07395e5

File tree

1 file changed

+7
-2
lines changed
  • files/en-us/web/javascript/reference/operators/addition

1 file changed

+7
-2
lines changed

files/en-us/web/javascript/reference/operators/addition/index.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,17 @@ false + false; // 0
7070
1n + 2n; // 3n
7171
```
7272

73-
You cannot mix BigInt and number operands in addition.
73+
You cannot mix BigInt and number operands in addition. `null`, `undefined`, and boolean values are coerced to numbers and are forbidden as well.
7474

7575
```js example-bad
7676
1n + 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions
7777
2 + 1n; // TypeError: Cannot mix BigInt and other types, use explicit conversions
78-
"1" + 2n; // TypeError: Cannot mix BigInt and other types, use explicit conversions
78+
```
79+
80+
Strings have priority over over types, so adding a string to a BigInt produces string concatenation rather than a `TypeError`.
81+
82+
```js
83+
"1" + 2n; // "12"
7984
```
8085

8186
To do addition with a BigInt and a non-BigInt, convert either operand:

0 commit comments

Comments
 (0)