-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
48 lines (33 loc) · 1.18 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
title: "SyntaxError: invalid BigInt syntax"
slug: Web/JavaScript/Reference/Errors/Invalid_BigInt_syntax
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript exception "invalid BigInt syntax" occurs when a string value is being coerced to a {{jsxref("BigInt")}} but it failed to be parsed as an integer.
## Message
```plain
SyntaxError: Cannot convert x to a BigInt (V8-based)
SyntaxError: invalid BigInt syntax (Firefox)
SyntaxError: Failed to parse String to BigInt (Safari)
```
## Error type
{{jsxref("SyntaxError")}}.
## What went wrong?
When using the [`BigInt()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt) function to convert a string to a BigInt, the string will be parsed in the same way as source code, and the resulting value must be an integer value.
## Examples
### Invalid cases
```js example-bad
const a = BigInt("1.5");
const b = BigInt("1n");
const c = BigInt.asIntN(4, "8n");
// SyntaxError: invalid BigInt syntax
```
### Valid cases
```js example-good
const a = BigInt("1");
const b = BigInt(" 1 ");
const c = BigInt.asIntN(4, "8");
```
## See also
- [`BigInt`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)