var x = "<%= x %>"; var y = 5; Second line is treated as if it was a string. Problem with multiple lines inside ruby if: <% if b %> var x = 1; alert(1); <% end %>