aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/errors/no_variable_name/index.html
blob: 4ff3b8090eab846a6e8dfd94c141a0c3f73590e3 (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
76
77
78
79
80
81
82
83
---
title: 'SyntaxError: missing variable name'
slug: Web/JavaScript/Reference/Errors/No_variable_name
tags:
  - Error
  - Error de sintaxis
  - errores
translation_of: Web/JavaScript/Reference/Errors/No_variable_name
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="Mensaje">Mensaje</h2>

<pre class="syntaxbox">SyntaxError: missing variable name (Firefox)
SyntaxError: Unexpected token = (Chrome)</pre>

<h2 id="Error">Error </h2>

<p>{{jsxref("SyntaxError")}}</p>

<h2 id="¿Qué_anda_mal">¿Qué anda mal?</h2>

<p>Una variable no tiene nombre. Esto es un error recurrente en el código. Probablemente una coma está mal colocada en algún lugar o nombraste mal una variable. Completamente entendible. Nombrar cosas es difícil.</p>

<h2 id="Ejemplos">Ejemplos</h2>

<h3 id="Missing_a_variable_name">Missing a variable name</h3>

<pre class="brush: js example-bad">var = "foo";
</pre>

<p>Es bueno nombrar buenas variables. Todos hemos estado así.</p>

<pre class="brush: js example-good">var ohGodWhy = "foo";</pre>

<h3 id="Palabras_reservadas_no_pueden_ser_nombre_de_variables">Palabras reservadas no pueden ser nombre de variables</h3>

<p>Hay algunas palabras que son reservadas. No puedes usarlas para nombrar variables, lo siento :(</p>

<pre class="brush: js example-bad">var debugger = "whoop";
// SyntaxError: missing variable name
</pre>

<h3 id="Declarar_multiples_variables">Declarar multiples variables</h3>

<p>Toma especial atención a las comas cuando declaras multiples variables. ¿Hay un exceso de comas? ¿Accidentalmente añadiste comas en vez de punto y coma? </p>

<pre class="brush: js example-bad">var x, y = "foo",
var x, = "foo"

var first = document.getElementById('one'),
var second = document.getElementById('two'),

// SyntaxError: missing variable name
</pre>

<p>La versión arreglada:</p>

<pre class="brush: js example-good">var x, y = "foo";
var x = "foo";

var first = document.getElementById('one');
var second = document.getElementById('two');</pre>

<h3 id="Arrays">Arrays</h3>

<p>{{jsxref("Array")}} literales en JavaScript necesitan corchetes alrededor de los valores. Esto no funciona:</p>

<pre class="brush: js example-bad">var arr = 1,2,3,4,5;
// SyntaxError: missing variable name
</pre>

<p>Esto es correcto:</p>

<pre class="brush: js example-good">var arr = [1,2,3,4,5];</pre>

<h2 id="Ver_también">Ver también</h2>

<ul>
 <li><a href="http://wiki.c2.com/?GoodVariableNames">Good variable names</a></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declarations">Variable declarations in the JavaScript Guide</a></li>
</ul>