How to insert javascript variable inside ejs tags

I am doing dynamic filtering ,but i am unable to do if condition check.

i want to insert targetOS var inside <%if(data[i].ListNames == targetOS ){%>

for the above syntax it is giving "targetOS variable is undefined"

Please help on this.

 $(document).on("change",'.COTSCurrentOSClass', function(e){  
  var selectedOS   = $(this).val();    
  var targetOS =  "OS::From::"+selectedOS;
  
  <%for(var i=0;i<data.length;i++){%>
   <%if(data[i].ListNames == ***targetOS*** ){%>
    <%for(var j=0;j<data[i].Values.length;j++){%> 
    console.log("options are");
    console.log(data[i].Values[j]);
  <%}}}%>

  ($(this).parent().parent()).find(".COTSTargetOSClass").append("<option>Select One</option>");
  
 });

What you want to do is not possible!

This is the sequence of what happens:

1 - In the server, the ejs is rendered to HTML

2 - the HTML is then transferred into the browser

3 - The browser reads and processes the javascript

The JavaScript execution happens at a much later stage than the ejs execution.

What @fmsf has replied is correct. However, if you know the targetOS value on the server, while rendering the ejs you can do it like this:

define it like this in your ejs file:

var TARGET_OS = "<%= TARGET_OS %>";

Then when you render the page via ejs, pass parameter to it like this:

res.render(url, {TARGET_OS: "Your desired value here"}, function(err, html) {