Unexpected array behavior when passed through EJS in Express [duplicate]

You have to stringify the array

var test = <%- JSON.stringify(level_tab) %>;

I'm not familiar with EJS but in general the same principle should apply even if syntax is slightly different in EJS.


i found a solution it's work, but i don't know if there is other ways to do it. i change

var test = '<%- level_tab %>';

by this loop,

<% for(var j=0; j<level_tab.length; j++) { %>
            level_tab.push('<%- level_tab[j]%>');
<%}%>

Refer to JSON from EJS to JSON object in JS

Remove the single quote:

var test = <%- JSON.stringify(level_tab) %>;


Use this it will work fine.(Don't use single quotation )

        var test = <%-JSON.stringify(level_tab) %>; 
        console.log("test :"+test);