recursal/MDN huggingface dataset:
TypeError: "x" is read-only
===========================

 The JavaScript strict mode-only exception
 "is read-only" occurs when a global variable or object
 property that was assigned to is a read-only property.

Message
-------
```

TypeError: Cannot assign to read only property 'x' of #<Object> (V8-based)
TypeError: "x" is read-only (Firefox)
TypeError: Attempted to assign to readonly property. (Safari)
```

Error type
----------

`TypeError` in strict mode only.

What went wrong?
----------------

 The global variable or object property that was assigned to is a read-only property.
 (Technically, it is a non-writable data property.)

 This error happens only in strict mode code. In
 non-strict code, the assignment is silently ignored.

Examples
--------

### Invalid cases

 Read-only properties are not super common, but they can be created using
 `Object.defineProperty()` or `Object.freeze()`.


```js
"use strict";
const obj = Object.freeze({ name: "Elsa", score: 157 });
obj.score = 0; // TypeError

("use strict");
Object.defineProperty(this, "LUNG\_COUNT", { value: 2, writable: false });
LUNG\_COUNT = 3; // TypeError

("use strict");
const frozenArray = Object.freeze([0, 1, 2]);
frozenArray[0]++; // TypeError
```


 There are also a few read-only properties built into JavaScript. Maybe you tried to
 redefine a mathematical constant.


```js
"use strict";
Math.PI = 4; // TypeError
```


Sorry, you can't do that.

 The global variable `undefined` is also read-only, so you can't silence the
 infamous "undefined is not a function" error by doing this:


```js
"use strict";
undefined = function () {}; // TypeError: "undefined" is read-only
```

### Valid cases


```js
"use strict";
let obj = Object.freeze({ name: "Score", points: 157 });
obj = { name: obj.name, points: 0 }; // replacing it with a new object works
```

See also
--------

* `Object.defineProperty()`
* `Object.freeze()`
SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions
=============================================================================

The JavaScript exception "cannot use `??` unparenthesized within `||` and `&&` expressions" occurs when an nullish coalescing operator is used with a logical OR or logical AND in the same expression without parentheses.

Message
-------
```

SyntaxError: Unexpected token '??' (V8-based)
SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions (Firefox)
SyntaxError: Unexpected token '??'. Coalescing and logical operators used together in the same expression; parentheses must be used to disambiguate. (Safari)
```

Error type
----------

`SyntaxError`What went wrong?
----------------

The operator precedence chain looks like this:
```

|   >   &&   >   ||   >   =
|   >   ??   >   =
```


However, the precedence *between* `??` and `&&`/`||` is intentionally undefined, because the short circuiting behavior of logical operators can make the expression's evaluation counter-intuitive. Therefore, the following combinations are all syntax errors, because the language doesn't know how to parenthesize the operands:


```js
a ?? b || c;
a || b ?? c;
a ?? b && c;
a && b ?? c;
```


Instead, make your intent clear by parenthesizing either side explicitly:


```js
(a ?? b) || c;
a ?? (b && c);
```

Examples
--------

When migrating legacy code that uses `||` and `&&` for guarding against `null` or `undefined`, you may often convert it partially:


```js
function getId(user, fallback) {
  // Previously: user && user.id || fallback
  return user && user.id ?? fallback; // SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions
}
```


Instead, consider parenthesizing the `&&`:


```js
function getId(user, fallback) {
  return (user && user.id) ?? fallback;
}
```


Even better, consider using optional chaining instead of `&&`:


```js
function getId(user, fallback) {
  return user?.id ?? fallback;
}
```

See also
--------

* Issue about nullish coalescing precedence in the TC39 nullish-coalescing proposal
* Nullish coalescing operator (`??`)
* Operator precedence
TypeError: cyclic object value
==============================

 The JavaScript exception "cyclic object value" occurs when object references were found
 in JSON. `JSON.stringify()` doesn't try
 to solve them and fails accordingly.

Message
-------
```

TypeError: Converting circular structure to JSON (V8-based)
TypeError: cyclic object value (Firefox)
TypeError: JSON.stringify cannot serialize cyclic structures. (Safari)
```

Error type
----------

`TypeError`What went wrong?
----------------

 The JSON format per se doesn't support object
 references (although an IETF draft exists),
 hence `JSON.stringify()` doesn't try to solve them and fails accordingly.

Examples
--------

### Circular references

In a circular structure like the following


```js
const circularReference = { otherData: 123 };
circularReference.myself = circularReference;
```


`JSON.stringify()` will fail


```js
JSON.stringify(circularReference);
// TypeError: cyclic object value
```


 To serialize circular references you can use a library that supports them (e.g. cycle.js)
 or implement a solution by yourself, which will require finding and replacing (or
 removing) the cyclic references by serializable values.

 The snippet below illustrates how to find and filter (thus causing data loss) a cyclic
 reference by using the `replacer` parameter of
 `JSON.stringify()`:


```js
function getCircularReplacer() {
  const ancestors = [];
  return function (key, value) {
    if (typeof value !== "object" || value === null) {
      return value;
    }
    // `this` is the object that value is contained in,
    // i.e., its direct parent.
    while (ancestors.length > 0 && ancestors.at(-1) !== this) {
      ancestors.pop();
    }
    if (ancestors.includes(value)) {
      return "[Circular]";
    }
    ancestors.push(value);
    return value;
  };
}

JSON.stringify(circularReference, getCircularReplacer());
// {"otherData":123,"myself":"[Circular]"}

const o = {};
const notCircularReference = [o, o];
JSON.stringify(notCircularReference, getCircularReplacer());
// [{},{}]
```

See also
--------

* `JSON.stringify()`
* cycle.js on GitHub
InternalError: too much recursion
=================================

 The JavaScript exception "too much recursion" or "Maximum call stack size exceeded"
 occurs when there are too many function calls, or a function is missing a base case.

Message
-------
```

RangeError: Maximum call stack size exceeded (Chrome)
InternalError: too much recursion (Firefox)
RangeError: Maximum call stack size exceeded. (Safari)
```

Error type
----------

`InternalError` in Firefox; `RangeError` in Chrome and Safari.

What went wrong?
----------------

 A function that calls itself is called a *recursive function*. Once a condition
 is met, the function stops calling itself. This is called a *base case*.

 In some ways, recursion is analogous to a loop. Both execute the same code multiple
 times, and both require a condition (to avoid an infinite loop, or rather, infinite
 recursion in this case). When there are too many function calls, or a function is
 missing a base case, JavaScript will throw this error.

Examples
--------

This recursive function runs 10 times, as per the exit condition.


```js
function loop(x) {
  if (x >= 10)
    // "x >= 10" is the exit condition
    return;
  // do stuff
  loop(x + 1); // the recursive call
}
loop(0);
```


Setting this condition to an extremely high value, won't work:


```js
function loop(x) {
  if (x >= 1000000000000) return;
  // do stuff
  loop(x + 1);
}
loop(0);

// InternalError: too much recursion
```


 This recursive function is missing a base case. As there is no exit condition, the
 function will call itself infinitely.


```js
function loop(x) {
  // The base case is missing
  loop(x + 1); // Recursive call
}

loop(0);

// InternalError: too much recursion
```

### Class error: too much recursion


```js
class Person {
  constructor() {}
  set name(name) {
    this.name = name; // Recursive call
  }
}

const tony = new Person();
tony.name = "Tonisha"; // InternalError: too much recursion
```


 When a value is assigned to the property name (this.name = name;) JavaScript needs to
 set that property. When this happens, the setter function is triggered.

In this example when the setter is triggered, it is told to do the same thing again: *to set the same property that it is meant to handle.* This causes the function to call itself, again and again, making it infinitely recursive.

This issue also appears if the same variable is used in the getter.


```js
class Person {
  get name() {
    return this.name; // Recursive call
  }
}
```


 To avoid this problem, make sure that the property being assigned to inside the setter
 function is different from the one that initially triggered the setter. The same goes
 for the getter.


```js
class Person {
  constructor() {}
  set name(name) {
    this._name = name;
  }
  get name() {
    return this._name;
  }
}
const tony = new Person();
tony.name = "Tonisha";
console.log(tony);
```

See also
--------

* Recursion
* Recursive functions
SyntaxError: missing = in const declaration
===========================================

 The JavaScript exception "missing = in const declaration" occurs when a const
 declaration was not given a value in the same statement (like
 `const RED_FLAG;`). You need to provide one
 (`const RED_FLAG = "#ff0"`).

Message
-------
```

SyntaxError: Missing initializer in const declaration (V8-based)
SyntaxError: missing = in const declaration (Firefox)
SyntaxError: Unexpected token ';'. const declared variable 'x' must have an initializer. (Safari)
```

Error type
----------

`SyntaxError`What went wrong?
----------------

 A constant is a value that cannot be altered by the program during normal execution. It
 cannot change through re-assignment, and it can't be redeclared. In JavaScript,
 constants are declared using the
 `const`
 keyword. An initializer for a constant is required; that is, you must specify its value
 in the same statement in which it's declared (which makes sense, given that it can't be
 changed later).

Examples
--------

### Missing const initializer

 Unlike `var` or `let`, you must specify a value for a
 `const` declaration. This throws:


```js
const COLUMNS;
// SyntaxError: missing = in const declaration
```

### Fixing the error

 There are multiple options to fix this error. Check what was intended to be achieved
 with the constant in question.

#### Adding a constant value

Specify the constant value in the same statement in which it's declared:


```js
const COLUMNS = 80;
```


#### `const`, `let` or `var`?

 Do not use `const` if you weren't meaning to declare a constant. Maybe you
 meant to declare a block-scoped variable with
 `let` or
 global variable with
 `var`. Both
 don't require an initial value.


```js
let columns;
```

See also
--------

* `const`
* `let`
* `var`
TypeError: can't redefine non-configurable property "x"
=======================================================

 The JavaScript exception "can't redefine non-configurable property" occurs when it was
 attempted to redefine a property, but that property is non-configurable.

Message
-------
```

TypeError: Cannot redefine property: "x" (V8-based)
TypeError: can't redefine non-configurable property "x" (Firefox)
TypeError: Attempting to change value of a readonly property. (Safari)
```

Error type
----------

`TypeError`What went wrong?
----------------

 It was attempted to redefine a property, but that property is non-configurable. The
 `configurable` attribute controls whether the property can be deleted from
 the object and whether its attributes (other than `writable`) can be changed.
 Usually, properties in an object created by an
 object initializer are configurable. However, for example, when using
 `Object.defineProperty()`, the property isn't configurable by default.

Examples
--------

### Non-configurable properties created by Object.defineProperty

 The `Object.defineProperty()` creates non-configurable properties if you
 haven't specified them as configurable.


```js
const obj = Object.create({});
Object.defineProperty(obj, "foo", { value: "bar" });

Object.defineProperty(obj, "foo", { value: "baz" });
// TypeError: can't redefine non-configurable property "foo"
```


 You will need to set the "foo" property to configurable, if you intend to redefine it
 later in the code.


```js
const obj = Object.create({});
Object.defineProperty(obj, "foo", { value: "bar", configurable: true });
Object.defineProperty(obj, "foo", { value: "baz", configurable: true });
```

See also
--------

* [[Configurable]]
* `Object.defineProperty()`
RangeError: repeat count must be less than infinity
===================================================

 The JavaScript exception "repeat count must be less than infinity" occurs when the
 `String.prototype.repeat()` method is used with a `count`
 argument that is infinity.

Message
-------
```

RangeError: Invalid string length (V8-based)
RangeError: Invalid count value: Infinity (V8-based)
RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox)
RangeError: Out of memory (Safari)
RangeError: String.prototype.repeat argument must be greater than or equal to 0 and not be Infinity (Safari)
```

Error type
----------

`RangeError`What went wrong?
----------------

 The `String.prototype.repeat()` method has been used. It has a
 `count` parameter indicating the number of times to repeat the string. It
 must be between 0 and less than positive `Infinity` and cannot be a negative
 number. The range of allowed values can be described like this: [0, +∞).

 The resulting string can also not be larger than the maximum string size, which can
 differ in JavaScript engines. In Firefox (SpiderMonkey) the maximum string size is
 230 - 2 (~2GiB).

Examples
--------

### Invalid cases


```js
"abc".repeat(Infinity); // RangeError
"a".repeat(2 \*\* 30); // RangeError
```

### Valid cases


```js
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (count will be converted to integer)
```

See also
--------

* `String.prototype.repeat()`
TypeError: invalid assignment to const "x"
==========================================

 The JavaScript exception "invalid assignment to const" occurs when it was attempted to
 alter a constant value. JavaScript
 `const`
 declarations can't be re-assigned or redeclared.

Message
-------
```

TypeError: Assignment to constant variable. (V8-based)
TypeError: invalid assignment to const 'x' (Firefox)
TypeError: Attempted to assign to readonly property. (Safari)
```

Error type
----------

`TypeError`What went wrong?
----------------

 A constant is a value that cannot be altered by the program during normal execution. It
 cannot change through re-assignment, and it can't be redeclared. In JavaScript,
 constants are declared using the
 `const`
 keyword.

Examples
--------

### Invalid redeclaration

Assigning a value to the same constant name in the same block-scope will throw.


```js
const COLUMNS = 80;

// …

COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'
```

### Fixing the error

 There are multiple options to fix this error. Check what was intended to be achieved
 with the constant in question.

#### Rename

 If you meant to declare another constant, pick another name and re-name. This constant
 name is already taken in this scope.


```js
const COLUMNS = 80;
const WIDE\_COLUMNS = 120;
```


#### const, let or var?

 Do not use const if you weren't meaning to declare a constant. Maybe you meant to
 declare a block-scoped variable with
 `let` or
 global variable with
 `var`.


```js
let columns = 80;

// …

columns = 120;
```


#### Scoping

 Check if you are in the correct scope. Should this constant appear in this scope or was
 it meant to appear in a function, for example?


```js
const COLUMNS = 80;

function setupBigScreenEnvironment() {
  const COLUMNS = 120;
}
```

### const and immutability

 The `const` declaration creates a read-only reference to a value. It does
 **not** mean the value it holds is immutable, just that the variable
 identifier cannot be reassigned. For instance, in case the content is an object, this
 means the object itself can still be altered. This means that you can't mutate the value
 stored in a variable:


```js
const obj = { foo: "bar" };
obj = { foo: "baz" }; // TypeError: invalid assignment to const `obj'
```


But you can mutate the properties in a variable:


```js
obj.foo = "baz";
obj; // { foo: "baz" }
```

See also
--------

* `const`
* `let`
* `var`
TypeError: More arguments needed
================================

 The JavaScript exception "more arguments needed" occurs when there is an error with how
 a function is called. More arguments need to be provided.

Message
-------
```

TypeError: Object prototype may only be an Object or null: undefined (V8-based)
TypeError: Object.create requires at least 1 argument, but only 0 were passed (Firefox)
TypeError: Object.setPrototypeOf requires at least 2 arguments, but only 0 were passed (Firefox)
TypeError: Object.defineProperties requires at least 1 argument, but only 0 were passed (Firefox)
TypeError: Object prototype may only be an Object or null. (Safari)
```

Error type
----------

`TypeError`.

What went wrong?
----------------

There is an error with how a function is called. More arguments need to be provided.

Examples
--------

### Required arguments not provided

 The `Object.create()` method requires at least one argument and the
 `Object.setPrototypeOf()` method requires at least two arguments:


```js
const obj = Object.create();
// TypeError: Object.create requires at least 1 argument, but only 0 were passed

const obj2 = Object.setPrototypeOf({});
// TypeError: Object.setPrototypeOf requires at least 2 arguments, but only 1 were passed
```


You can fix this by setting `null` as the prototype, for example:


```js
const obj = Object.create(null);

const obj2 = Object.setPrototypeOf({}, null);
```

See also
--------

* Functions guide
SyntaxError: illegal character
==============================

 The JavaScript exception "illegal character" occurs when there is an invalid or
 unexpected token that doesn't belong at this position in the code.

Message
-------
```

SyntaxError: Invalid character (Edge)
SyntaxError: illegal character (Firefox)
SyntaxError: Invalid or unexpected token (Chrome)
```

Error type
----------

`SyntaxError`What went wrong?
----------------

 There is an invalid or unexpected token that doesn't belong at this position in the
 code. Use an editor that supports syntax highlighting and carefully check your code
 against mismatches like a minus sign (`-`) versus a dash (`–`)
 or simple quotes (`"`) versus non-standard quotation marks (`"`).

Examples
--------

### Mismatched characters

 Some characters look similar, but will cause the parser to fail interpreting your code.
 Famous examples of this are quotes, the minus or semicolon
 (greek question mark (U+37e) looks same).


```js
“This looks like a string”; // SyntaxError: illegal character
// “ and ” are not " but look like it

42 – 13; // SyntaxError: illegal character
// – (en-dash) is not - but looks like it

const foo = "bar"; // SyntaxError: illegal character
// <37e> is not ; but looks like it
```


This should work:


```js
"This is actually a string";
42 - 13;
const foo = "bar";
```


Some editors and IDEs will notify you or at least use a slightly different highlighting for it, but not all. When something like this happens to your code and you're not able to find the source of the problem, it's often best to just delete the problematic line and retype it.

### Forgotten characters

It's easy to forget a character here or there.


```js
const colors = ["#000", #333", "#666"];
// SyntaxError: illegal character
```


Add the missing quote for `"#333"`.


```js
const colors = ["#000", "#333", "#666"];
```

### Hidden characters

 When copy pasting code from external sources, there might be invalid characters. Watch
 out!


```js
const foo = "bar";​
// SyntaxError: illegal character
```


 When inspecting this code in an editor like Vim, you can see that there is actually a
 zero-width space (ZWSP) (U+200B) character.


```js
const foo = "bar";<200b>
```

See also
--------

* Lexical grammar
SyntaxError: missing ) after condition
======================================

 The JavaScript exception "missing ) after condition" occurs when there is an error with
 how an
 `if`
 condition is written. It must appear in parenthesis after the `if` keyword.

Message
-------
```

SyntaxError: missing ) after condition (Firefox)
SyntaxError: Unexpected token '{'. Expected ')' to end an 'if' condition. (Safari)
```

Error type
----------

`SyntaxError`What went wrong?
----------------

 There is an error with how an
 `if`
 condition is written. In any programming language, code needs to make decisions and
 carry out actions accordingly depending on different inputs. The if statement executes a
 statement if a specified condition is truthy. In JavaScript, this condition must appear
 in parenthesis after the `if` keyword, like this:


```js
if (condition) {
  // do something if the condition is true
}
```

Examples
--------

### Missing parenthesis

It might just be an oversight, carefully check all you parenthesis in your code.


```js
if (Math.PI < 3 {
  console.log("wait what?");
}

// SyntaxError: missing ) after condition
```


To fix this code, you would need to add a parenthesis that closes the condition.


```js
if (Math.PI < 3) {
  console.log("wait what?");
}
```

### Misused is keyword

 If you are coming from another programming language, it is also easy to add keywords
 that don't mean the same or have no meaning at all in JavaScript.


```js
if (done is true) {
 console.log("we are done!");
}

// SyntaxError: missing ) after condition
```


 Instead you need to use a correct comparison operator.
 For example:


```js
if (done === true) {
  console.log("we are done!");
}
```


Or even better:


```js
if (done) {
  console.log("we are done!");
}
```

See also
--------

* `if...else`
* Relational operators
* Making decisions in your code — conditionals