aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/global_objects/syntaxerror/index.md
blob: eadce42a6e42fb9102edc77ff10a2e73787582ee (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: SyntaxError
slug: Web/JavaScript/Reference/Global_Objects/SyntaxError
translation_of: Web/JavaScript/Reference/Global_Objects/SyntaxError
original_slug: Web/JavaScript/Referencia/Objetos_globales/SyntaxError
browser-compat: javascript.builtins.SyntaxError
---
{{JSRef}}

El objeto **`SyntaxError`** represta un error cuando se trata de interpretar código que resulta ser inválido sintácticamente. Esto ocurre cuando el motor de JavaScript encuentra el orden de los tokens o los token mismos no son conformes a la sinstaxis del lenguaje cuando se analiza el código.

## Constructor

- {{jsxref("Global_Objects/SyntaxError/SyntaxError", "SyntaxError()")}}
  - : Crea un nuevo objeto `SyntaxError`.

## Propiedades de instancia

- {{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}
  - : Mensaje de error. Heredado de {{jsxref("Error")}}.
- {{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}}
  - : Ruta al archivo que ha provocado este error. Heredado de {{jsxref("Error")}}.
- {{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}}
  - : Número de línea en el archivo que ha provocado este error. Heredado de {{jsxref("Error")}}.
- {{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}}
  - : Número de columna en el archivo que ha provocado este error. Heredado de {{jsxref("Error")}}.
- {{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}}
  - : Trazado de la pila de ejecución. Heredado de {{jsxref("Error")}}.

## Ejemplos

### Controlando un SyntaxError

```js
try {
  eval("hoo bar");
} catch (e) {
  console.error(e instanceof SyntaxError);
  console.error(e.message);
  console.error(e.name);
  console.error(e.fileName);
  console.error(e.lineNumber);
  console.error(e.columnNumber);
  console.error(e.stack);
}
```

### Creando un SyntaxError

```js
try {
  throw new SyntaxError("Hello", "someFile.js", 10);
} catch (e) {
  console.error(e instanceof SyntaxError); // true
  console.error(e.message); // Hello
  console.error(e.name); // SyntaxError
  console.error(e.fileName); // Ejemplo.js
  console.error(e.lineNumber); // 10
  console.error(e.columnNumber); // 0
  console.error(e.stack); // @debugger eval code:3:9
}
```

## Especificaciones

{{Specifications}}

## Compatibilidad con navegadores

{{Compat}}

## Véase también

- {{jsxref("Error")}}