title: Escape String - Output rails string in Javascript
body: <p>I'm trying to assign a string value to a javascript object in my .erb file like so:</p>
  
  <pre><code>var data = {
      'name': '&lt;%= @product.name %&gt;',
      ...
  };
  </code></pre>
  
  <p>The problem is, if the value of <code>name</code> is <code>Tom's small ears</code>, </p>
  
  <p>the output of <code>data.name</code> would be <code>Tom&amp;#x27;s small ears</code>.</p>
  
  <p>Is there a way to escape special characters?</p>
  
  <p>I tried doing <code>'name': '&lt;%= raw @product.name %&gt;'</code> but <code>Uncaught SyntaxError: Unexpected identifier</code> gets output into the console.</p>
  
  <p>Doing <code>&lt;%= escape_javascript @product.name %&gt;</code> outputs <code>Tom\&amp;#x27;s small ears</code></p>
  
  <p><strong>Edit</strong>
  @Stefan's comment under <code>MrYoshiji's</code> answer worked for me.</p>