title: How to write to CSV file in Javascript
body: <p>I have a script (using PhantomJS) that tests how long it takes to load a webpage. What I am trying to figure out is how to write the result of time taken to load the page to a .csv file. Then if I were to re-run the test again for it to add another result to the .csv file.</p>
  
  <p>code:</p>
  
  <pre><code>var page = require('webpage').create(),
      system = require('system'),
      t, address;
  var pageLoadArray = [];
  var csvContents = "";
  fs = require('fs');
  
  if (system.args.length === 1) {
      console.log('Usage: loadspeed.js &lt;some URL&gt;');
      phantom.exit(1);
  } else {
      t = Date.now();
      address = system.args[1];
      page.open(address, function (status) {
          if (status !== 'success') {
              console.log('FAIL to load the address');
          } 
          else {
              t = Date.now() - t;
              console.log('Page title is ' + page.evaluate(function () {
                  return document.title;
              }));
  
              if(t&gt;7000){
                  console.log('Loading time was too long... ' + t + "msec");
                  pageLoadArray.push(t);
                  console.log(pageLoadArray.length);
                  console.log(pageLoadArray[0]);
                  //store the time value to the .csv file
                  phantom.exit(1);
              }
              else{
                  console.log('Loading time ' + t + ' msec');
                  pageLoadArray.push(t);
                  console.log(pageLoadArray.length);
                  console.log(pageLoadArray[0]);
                  //store the time value to the .csv file
              }
          }
          phantom.exit();
      });
  
  }
  </code></pre>