I want to extract a value from a variable. If its an array, I want the first element, and if not, I want the value of that variable. The value is a float
, but I was wondering which of these are better in terms of performance, portability to non-floats, and of course short code and code readability
I want to use
value = variable[0] || variable
Its short and nice, is there any caveats is using this?
or I can do,
value = ([].concat(variable))[0]
This SO question says it's bad for performance.
And then, ofcourse, I can check if variable
is an array or not in a few different ways, that is also mentioned in above question. Is there any better ways if the first one is not good?