title: Replace a variable in a function with the value in javascript
body: <p>I am looking for a way to replace a variable in a function with its actual value. I am going to convert this function into a string and send via a HTTP request and thus need to convert the variables inside the function with their values.</p>
  
  <pre><code>let x = Math.random();
  
  let funcString = function () {
      let y = x + 10;
      return y;
  }.toString();
  
  // Send funcString as a parameter
  </code></pre>
  
  <p>For eg. in the above code if I send <code>funcString</code> as it is, whoever receiving it will have no idea what is the value of <code>x</code>. </p>
  
  <p>Since I am ultimately sending a string I would like to send </p>
  
  <blockquote>
    <p><code>"function () {let y = 0.53 + 10; return y;}"</code> (assuming
    <code>Math.random()</code> produced 0.53 at runtime).</p>
  </blockquote>
  
  <p>Is there any way to do this? </p>
  
  <p>I am doing this in a nodejs project so a npm module would be fine by me too.</p>