Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected EOF in JS

Tags:

javascript

I have the following script:

function getMoods(nb) {
    var index;
    var a = ["Banana", "Coconut", "Peach", "Apple", ...];
    for (index=0; index<nb; ++index) {
        alert('a');
        if(index==1 || index==5 || index==9 || index==13) { moods += '<div class="col-xs-4">'; }
            moods += '
                <div class="checkbox">
                    <label for="'+a[index]+'">
                        <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'
                    </label>
                </div>';
        if(index==4 || index==8 || index==12) { moods += '</div> '; }
    }
    $("#moods-area").html(moods);
}

I do not understand why I have the following error:

SyntaxError: Unexpected EOF

Could you please help me ?

like image 840
Geronimo Avatar asked Oct 27 '25 23:10

Geronimo


1 Answers

There are two problems:

  1. A wrong use of the spread operator ...:

    ["Banana", "Coconut", "Peach", "Apple", ...];
    

    This throws SyntaxError: expected expression, got ']', because after the spread operator there isn't any iterable object.

  2. JavaScript doesn't support multiline strings.

    You can use some alternatives:

    • Concatenate multiple strings

      moods += 
        '<div class="checkbox">'
          +'<label for="'+a[index]+'">'
            +'<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]
          +'</label>'
        +'</div>';
      
    • Use \ at the end of each line to continue the string at the next one

      moods += '\
        <div class="checkbox">\
          <label for="'+a[index]+'">\
            <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'\
          </label>\
        </div>';
      
    • Join an array of strings:

      moods += [
        '<div class="checkbox">',
          '<label for="'+a[index]+'">',
            '<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index],
          '</label>',
        '</div>'].join('');
      
like image 127
Oriol Avatar answered Oct 30 '25 15:10

Oriol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!