var MM = '\' + obj[0]['MM '] + '/';
I get two errors while using this code...
missing; before statement and unterminated string literal
var MM = '\' + obj[0]['MM '] + '/';
I get two errors while using this code...
Share Improve this question edited Aug 7, 2011 at 14:19 John Cooper asked Aug 7, 2011 at 7:10 John CooperJohn Cooper 7,64133 gold badges83 silver badges102 bronze badges 0missing; before statement and unterminated string literal
4 Answers
Reset to default 7The character \
is "special" because it's used to allow the use of all printable characters in strings. In your case '\'
is not a string posed by the only character \
, but the beginning of a string starting with the single quote character '
.
For exampe if you want the string Hello Andrea "6502" Griffini
you can use single quotes
string1 = 'Hello Andrea "6502" Griffini';
and if you want single quotes in the string you can do the opposite
string2 = "Hello Andrea '6502' Griffini";
But what if you want both kind of quotes in the same string? This is where the escape \
character es handy:
string3 = "'llo Andrea \"6502\" Griffini";
Basically \
before a quote or double quote in a string tells javascript that the following character is just a regular character, with no special meaning attached to it.
Note that the very same character is also used in regular expressions... for example if you want to look for an open bracket [
you must prefix it with a backslash because [
in a regular expression has a special meaning.
The escape is also used to do the opposite... in a string if you put a backslash in front of a normal character you are telling javascript that that character is indeed special... for example
alert("This is\na test");
In the above line the \n
sequence means a newline code, so the message displayed will be on two lines ("This is" and "a test").
You may now wonder... what if I need a backslash character in my string? Just double it in that case. In your code for example just use '\\'
.
Here is a table for the possible meanings of backslash in strings
\" just a regular double-quote character, it doesn't end the string
\' just a regular single-quote character, it doesn't end the string
\b a backspace character (ASCII code 0x08)
\t a tab character (ASCII code 0x09)
\n a newline character (ASCII code 0x0A)
\v a vertical tab character (ASCII code 0x0B)
\f a form feed character (ASCII code 0x0C)
\r a carriage return character (ASCII code 0x0D)
\033 the character with ASCII code 033 octal = 27 ("ESC" in this case)
\x41 the character with ASCII code 0x41 = 65 ("A" in this case)
\u05D0 the unicode character 0x05D0 (Aleph from the Hebrew charset)
\\ just regular backslash character, not an escape prefix
\
is an escape character. You'll have to double it to literally mean a backslash character, otherwise it'll augment the following character (In this case the next single quote)
You need to properly escape the backslash:
var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';
Being escape character, the JS "piler" is expecting another character to follow, for example \n
is newline constant, \t
is tab etc.. so \\
is one single backslash in a string.
It is also mentioned in Douglas Crockford book.
You are forgetting to escape '\'
Do this:
var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';