You should be injecting any values you need for client-side use into the page, not into the JavaScript. If you use clever script techniques (e.g. generate a JS file from a view), you void all possible caching of the "page" (script) which has serious implications for commercial deployment of a website.
Typically you would use an input type="hidden" or inject data- attribute values into a key DOM element (like body). You can pick them up very simply from JavaScript/jQuery:
1. Hidden field
e.g. for a hidden field with id="currentuser"
HTML:
<input id="currentuser" type="hidden" value="<% =CurrentUser %>"/>
JQuery:
$(document).ready(function (){
var _currentUser = $('#currentuser').val();
...
});
2. data- attribute
or add the value as a data- attribute on a key DOM element e.g. on the body element
HTML:
<body data-currentuser="<% =CurrentUser %>">
JQuery:
$(document).ready(function (){
var _currentUser = $('body').data('currentuser');
...
});
3. Inject global variable in the page
Worst-case, have a small snippet of Javascript at the top of the page that injects a global variable value:
HTML:
<script>
window.currentuser="<% =CurrentUser %>";
</script>
JQuery:
$(document).ready(function (){
var _currentUser = window.currentuser; // or just currentuser
...
});
I use this last technique in my Razor layout file for one simple purpose only. Injecting the site root URL on the current website, so that it is available for any relative Ajax calls:
e.g. in Razor layout file
<script>
window.siteRoot="@Url.Content("~/")";
</script>