-1

Hi I get "Uncaught SyntaxError: Unexpected token ILLEGAL" error, when I run this code

str += "{'value': "+ response_imdb[count] +",
      'color':#"+ colorValue +",
      'label': "+ response_labels[count] +"
     }";

Thanks.

3
  • 1
    If you copy/pasted that code from somewhere, the problem is likely to be a stray invisible character somewhere in the text. Try typing the text in by hand. edit or the newline; gee I'm blind sometimes :)
    – Pointy
    Commented Dec 31, 2015 at 20:02
  • does it say the line and token?
    – user2486953
    Commented Dec 31, 2015 at 21:08
  • This question is similar to: How do I break a string across more than one line of code in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – dumbass
    Commented Jan 16 at 19:45

3 Answers 3

2

Here is another easy way.

str += JSON.stringify({
  value: response_imdb[count],
  color: '#' + colorValue,
  label: response_labels[count]
});
         

2
  • It's a nice way to do it, but it doesn't really answer the question. Commented Dec 31, 2015 at 20:05
  • The answer was already posted by the time I posted the answer. I thought this was a good aside.
    – synthet1c
    Commented Dec 31, 2015 at 20:06
2

In JavaScript you cannot have multi-line strings (unless you add a backslash to the end of each line).

Make them multiple strings and concatenate them using +, like so:

str += "{'value': "+ response_imdb[count] +"," +
  "'color':#"+ colorValue +"," +
  "'label': "+ response_labels[count] +
 "}";
2

Javascript doesn't allow line breaks in strings. You heave line breaks after ", at the end of each line. You should change it to:

str += "{'value': "+ response_imdb[count] +",\n"+
      'color':#"+ colorValue +",\n"+
      'label': "+ response_labels[count] +",\n"+
     }";

But it's almost always wrong to try to create JSON strings by hand. Use a function for it, like JSON.stringify in Javascript, json_encode in PHP, etc.

There are some other problems there. If the string is going to be parsed as JSON, it the property names need to be in double quotes, not single quotes. And # + colorValue needs to be in quotes to be a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.