Get array from variable in javascript
<p>I'm looking to get an array from a variable. If it's not already an array I want to return a new array with the variable as the only entry.Example:</p>

<pre><code>toArray('test'); // =&gt; ["test"]
toArray(['test']); // =&gt; ["test"]
</code></pre>

<p>My actual working code is:</p>

<pre><code>var toArray;

toArray = function(o) {
  if (Array.isArray(o)) {
    return o.slice();
  } else {
    return [o];
  }
};
</code></pre>

<p>I'd like to know if there is a nicer way for that (native or with underscore.js for example).</p>

<p>In ruby, you can do:</p>

<pre><code>Array('test') # =&gt; ["test"]
Array(['test']) # =&gt; ["test"]
</code></pre>
