Uncaught Syntax error when assigning Object values in Javascript

Asked

Viewed 42 times

2

I have the following code:

var modelo = 'Yamaha Fazer 250';
var fabricante = 'Yamaha';
var moto = {'Modelo - '+ modelo: fabricante};

I’m getting the following error code:

VM200:3 Uncaught Syntaxerror: Unexpected token +

  • I don’t think you can concatenate the attribute name there, you could do with the value: {'Modelo': modelo + fabricante}

  • 1

    but you can use the conchetes like this: ['Modelo - '+ modelo] : fabricante

2 answers

2


The problem is on the following line:

var moto = {'Modelo - '+ modelo: fabricante};

put keys to resolve this incident:

var modelo = 'Yamaha Fazer 250';
var fabricante = 'Yamaha';
var moto = {['Modelo - '+ modelo]: fabricante};

Source: Computed properties

2

Hello, little headlight!

The warning

VM200:3 Uncaught SyntaxError: Unexpected token +

It says that there is an unexpected token, in which case it is the plus sign. You cannot concatenate the name of your attribute, just its value (the one on the right). To resolve, do the following

var modelo     = 'Fazer 250';
var fabricante = 'Yamaha';
var moto       = { modelo: fabricante + modelo };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.