How to bypass signle quote error in javascript?

40 views Asked by At

I need to have an array as following but the single quote before (s) causes error. JavaScript does not accept them. Is there any way to bypass the error or replace the single quotes with other characters like space?

I tried to use replace function but not sure how to use it. I used ' but it did not work.

 var locations = [
    [
      'Alex's loc', '37.9908372',
      '23.7383394', '0'
    ],
    [
      'John James's loc', '37.9908372',
      '23.7383394', '1'
    ],
    [
      'Norman's loc', '38.075352',
      '23.807885', '3'
    ],
    [
      'Jack Moore's loc', '37.9908372',
      '23.7383394', '2'
    ]
  ];

Code

var locations = [
                <c:forEach var="location" items="${locationes}" varStatus="loop">[
                        '${location.value.name}', '${location.value.latitude}',
                        '${location.value.longitude}', '${loop.index}', </c:forEach> ];
2

There are 2 answers

0
Yangguang On BEST ANSWER

you can wrap the single quote with double quote, like:

var locations = [
    [
        "Alex's loc", '37.9908372',
    ]
];
0
t3dodson On

Using a backslash \ character before the invalid quote will also work. This is called an escape sequence

'Alex\'s loc'  // this is represented as the string => Alex's loc

This is also how you get litteral new lines in strings

'\n' or "\n" for new line.