Get value in JS sent by NodeJS route

I want to capture the value from a variable that it is sent from NodeJS route to a view. I know that the way to get this value on HTML (Jade) is:

#{name_var}

But I want to get this value in JS (JQuery) and the only way that I can think is by DOM (for example: create a div with this value that it is hidden and later, get the value by getElementById).

But I think that this data that I send to the view are delicated, it could be have a great security problem...

What do you think? Are there any other 'alternative' for this? Thank you.

I think you dont want to go through the hard way of putting the value first to a dom element and fetch it later to process.

You can directly assign the values to js variable like the same way you did in your html tags.

Like

script(type='text/javascript').
    var somevar = "#{serversideValue}";

So that while the html is rendered out of the jade template, it will be rendered as

script(type='text/javascript').
    var somevar = "passedValue";

Also according to the value passed from the router, you can choose whether to use html encoding or not while assigning the value to the js variable like #{serversideValue} or !{serversideValue}.

Also if you are passing a complete json object via the router variable, then you cannot directly assign it like what we seen above. One way is to JSON.stringify() the value when you send it from the router and use JSON.parse() in clientside JS to get back the original object.

Also You might want to look at this stackoverflow question to get an understanding of the scenario that we discussed now.