title: Using javascript variable in EJS
body: <p>I am learning node and javascript and need your help in using javascript variable in my EJS file.</p>
  
  <p>On page load, I am making an ajax call to get me some values. I want to display that on the page once the call is successful.</p>
  
  <p>Please find my EJS code below.</p>
  
  <pre><code>&lt;div id="tab2" class="tab"&gt;
      Page 2 am in friends page
      &lt;script type="text/javascript"&gt;
          if (currentFriend) { // currentFriend is the javascript variable
              document.write('inside if ');
              for (var i = 0; i &lt; currentFriend.length; i++) {
                  document.write(' Friend Name : "' + currentFriend[i] + '" &lt;br /&gt;' + );
              }
          } else {
              document.write('inside else ');
          } %
          &gt;
      &lt;/script&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I can see any output from the script tag ie. either <code>'inside if'</code> nor <code>'inside else'</code> is getting printed</p>
  
  <p>I have even tried </p>
  
  <pre><code>&lt;div id="tab2" class="tab"&gt;
       Page 2 am in friends page
       &lt;%  if(currentFriend){ // currentFriend is the javascript variable
  
              for(var i=0;i&lt;currentFriend.length;i++){ %&gt;
               Friend Name : &lt;%=currentFriend[i]%&gt;           }
        &lt;%} else { %&gt;
                inside else
        &lt;%}%&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>But this is giving me an error which says currentFriend is not defined.</p>
  
  <p>Please assist.</p>