How do I print a value from Craft/Twig into a Javascript function?
That is the correct way of doing it but you need to be sure that entry is a variable and set to an entry model at that point.  And that you're in an appropriate part of your template in general for outputting the code.

E.g. this works for analytics for example, placed at the end of my main structure block.

{# ANALYTICS GOOGLE #}
{% set js %}
&lt;!-- Google Code for Website Conversions Conversion Page --&gt;
&lt;script type="text/javascript"&gt;
/* &lt;![CDATA[ */
var google_conversion_id = XXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXX";
var google_conversion_value = {{order.totalPrice}};
var google_conversion_currency = "AUD";
var google_remarketing_only = false;
/* ]]&gt; */
&lt;/script&gt;
&lt;script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"&gt;
&lt;/script&gt;
&lt;noscript&gt;
&lt;div style="display:inline;"&gt;
&lt;img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXX/?value={{order.totalPrice}}&amp;amp;currency_code=AUD&amp;amp;label=XXXXXXXXX&amp;amp;guid=ON&amp;amp;script=0"/&gt;
&lt;/div&gt;
&lt;/noscript&gt;

{% endset %}

{% includejs js %}