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];