0

Can anyone help me what this small piece of code is doing in JavaScript? Actually, I am getting data from express server in Json form and when I render my page I want to access that data in my client JavaScript and this solution I got from another question in Stack Overflow (Accessing Express.js local variables in client side JavaScript), but I am not able to understand this syntax.

var local_data =!{JSON.stringify(data)};

I am trying to interpret this but cant understand. I am using handlebars as my template engine.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Haroon Ali
  • 11
  • 4
  • Hello, are you sure there are curly braces on the right hand side {JSON.stringify(data)} – Bhaskar Choudhary Feb 17 '19 at 19:41
  • Exclamation mark simply inverts the result of the right hand side. `var local_data = !false; //local_data will be true var local_data = !true; //local_data will be false` – Bhaskar Choudhary Feb 17 '19 at 19:43
  • The code you've should is IMHO not executable. I've tried it multiple times. Could you check this please? – Michael W. Czechowski Feb 17 '19 at 19:43
  • Not a valid expression at all – Code Maniac Feb 17 '19 at 19:43
  • 1
    Where did this line come from and are you sure you aren't getting an error from it? The statement doesn't make a lot of sense. If you wanted to create an object, you would use `JSON.parse()`, not `JSON.stingify()` and there wouldn't be a need for the `{}`. Also, the `!` in front of the object would always return `false` as long as a valid object was created. – Scott Marcus Feb 17 '19 at 19:43
  • The `!` will invert the truth value of the expression. So if it is `true` it will become `false` and vice versa. The only use I can imagine is if you try to `JSON.parse(undefined)` your result will be `undefined` and if you invert it, it will become `true`. It could be used to do some edge case testing if someone provides an undefined object. But there are better ways to do that...so the reason for all of this remains mysterious to me. – Alberti Buonarroti Feb 17 '19 at 19:48
  • actually i m getting data from express server in json form and i want to get that data in my client side javascript for which i tried to search on stack overflow but i got this type of expression for parse data in client javascript . but i am not being able to undertstand this. i m using handlebars as my template engine – Haroon Ali Feb 17 '19 at 19:50
  • this must be !JSON.stringify(data)... It interprets to if stringified `data` object exists or not. From strigified, we means `data` must be object and we typecast it to string. – Jaspreet Singh Feb 17 '19 at 19:50
  • @HaroonAli Can you edit that information into your question? – Brendon Shaw Feb 17 '19 at 19:58
  • `i want to get that data in my client side javascript` ...that's a fairly simple and common requirement. `Json.parse()` should be sufficient for doing that. It will turn a JSON string into a usable JavaScript variable. I don't know what youv'e been searching for but the code above is really strange, to say the least. – ADyson Feb 17 '19 at 20:07
  • 1
    If you simply want to process the JSON data you are receiving, all you need to do is: `let obj = JSON.parse(data);`. After that, you will have a JavaScript object and can do whatever you want with it. The code you've shown is nonsensical. – Scott Marcus Feb 17 '19 at 20:07
  • There are plenty of clues on that page that what you copied is not JavaSCript, like the fact that people are talking about "Jade templates", the answer begins "Using Jade templating..." the instructions are to put this code it in your `views/index.jade`, and the full context of the code is `!!! 5` `script(type='text/javascript')` `var local_data =!{JSON.stringify(data)}`, the first two lines of which are not JavaScript either. – Raymond Chen Feb 17 '19 at 20:18

1 Answers1

4

The question where you took that from code indicates that the code is part of a Jade template. I searched around for this exact line of code and found it referenced here and here, both of which also reference Jade.

In other words, this isn't valid JavaScript, but it is valid Jade. In this case, !{...} are Jade's syntax for unescaped string interpolation.

The equivalent in Handlebars.js is {{{...}}}, so try this:

var local_data = {{{JSON.stringify(data)}}};
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331