Cast string as array
javascript
Solution
JavaScript is a prototyping language and does not have a type casting system.
One solution would be to check if your variable is a string and convert it into an array. For example :
if (typeof someVariable === 'string') someVariable = [someVariable];
In PHP, if you do a check on a string, like (ex: `$array = 'string';`) :
$array = (array) $array; // ex: "string" becomes array("string")
The JavaScript equivalent will be
arr = typeof arr === 'string' ? [arr] : arr;
If your variable `arr` is not necessarily a string, you may use `instanceof` (edit: or `Array.isArray`) :
arr = arr instanceof Array ? arr : [arr];
arr = Array.isArray(arr) ? arr : [arr];
Problem
How, in Javascript, can I cast a string as an array in the same way that PHP (array) does. ``` //PHP $array = (array)"string" ``` Basically I have a variable that can be an array or a string and, if a string, I want to make it an array using an inline command.