title: Razor variable into Javascript
body: <p>I am trying to set a JavaScript variable from a model variable using Razor, and can't figure out what is wrong.
  In the CSHTML page, I've tried the following ways:</p>
  
  <pre><code>&lt;script&gt;
     var test1 = @Model.testVariable;
     var test2 = @(Model.testVariable);
     var test3 = &lt;text&gt;@Model.testVariable&lt;/text&gt;
     var test4 = @Html.Raw(Model.testVariable);
  &lt;/script&gt;
  </code></pre>
  
  <p>In the Model, test variable is defined like</p>
  
  <pre><code>public string testVariable { get; set; }
  </code></pre>
  
  <p>In the controller, I am setting the variable like:</p>
  
  <pre><code>model.testVariable = "x";
  </code></pre>
  
  <p>Then when I access the variable, I almost always get the error <code>test1 is undefined</code>. If I set the variable like <code>var test1 = 'x'</code>, then it works fine. It is only having a problem when I am grabbing the variable from the model</p>
  
  <pre><code>$(document).ready(function () {
     alert(test1);
  }
  </code></pre>