Syntaxerror: Unexpected token: string literal -Javascript

Asked

Viewed 618 times

0

I’m having this error in the following function:

function formaQueryConta(cpf){

        var numConta= jsonDtContasCliente[(document.getElementById("listaContas").value)-1];
        console.log(typeof(cpf)); //Diz que é String
        console.log(typeof(numConta)); //Diz que é String
        var parms = "&cpf="+cpf"&conta="+numConta; //O ERRO É APONTADO NESTA LINHA
        ajaxCall("Persistencia.php?action=buscaConta" +parms, formaCanvas);

}

I couldn’t find anything in Stack to help me. Some light?

1 answer

2


Missing one + in concatenation:

var parms = "&cpf="+cpf"&conta="+numConta; //O ERRO É APONTADO NESTA LINHA
//                     ^---aqui

I couldn’t find anything in Stack to help me

This is normal, because this error can be for several reasons and is more or less generic. It simply indicates that when you were constructing the string you found something you weren’t expecting, in this case the ".

What I advise for the next few times and try to read the line calmly and pay attention to each character.

A common alternative nowadays at ES6 is to use literals template, which works as interpolation and which I personally find more readable. In that case would look like this:

var parms = `&cpf=${cpf}&conta=${conta}`;

Browser other questions tagged

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