Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnologia Web para desenvolvedores
  2. JavaScript
  3. Referência JavaScript
  4. JavaScript error reference
  5. SyntaxError: missing variable name

Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.

View in EnglishAlways switch to English

SyntaxError: missing variable name

Mensagem

SyntaxError: missing variable name (Firefox)SyntaxError: Unexpected token = (Chrome)

Tipo de erro

SyntaxError

O que deu errado?

O nome de uma variável está faltando. Isto é provavelmente devido a um erro de sintaxe no seu código. Provavelmente uma vírgula está errada em algum lugar. Totalmente compreensível! Nomear as coisas é tão difícil.

Exemplos

Falta um nome de variável

js
var = "foo";

É difícil chegar com bons nomes de variáveis. Nós todos estivemos lá.

js
var ohGodWhy = "foo";

Palavras-chave reservadas não podem ser nomes de variáveis

Existem alguns nomes de variáveis que são palavras-chave resevadas. Você não pode usar isso. Desculpa :(

js
var debugger = "whoop";// SyntaxError: missing variable name

Declarando múltiplas variáveis

Preste especial atenção às vírgulas ao declarar múltiplas variáveis. Existe um excesso de vírgula? Você acidentalmente adicionou vírgulas em vez de ponto e vírgula?

js
var x, y = "foo",var x, = "foo"var first = document.getElementById('one'),var second = document.getElementById('two'),// SyntaxError: missing variable name

A versão corrigida:

js
var x,  y = "foo";var x = "foo";var first = document.getElementById("one");var second = document.getElementById("two");

Arrays

Array literais em JavaScript precisam de colchetes ao redor dos valores. Isso não funcionará:

js
var arr = 1,2,3,4,5;// SyntaxError: missing variable name

Isso seria correto:

js
var arr = [1, 2, 3, 4, 5];

Veja também

  • Bons nomes de variáveis
  • var
  • Declarações de variáveis no Guia JavaScript

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp