Let's say I have a single element array ["someStringHere"] (I don't know in advance what the string will be) and I want to access the string so that my function returns the string and not the array, how could I do so without using an indexer (ex: array[0])?
In other words, it would be as if I could delete the brackets so that I just had the string.
2 Answers 2
Every object has a toString method. You could go about using this method, though it's confusing why you'd want to. Another option is the join method.
var someArr = ['some string here'];
console.log(someArr.toString());
console.log(someArr.join());
According to MDN:
The Array object overrides the toString method of Object. For Array objects, the toString method joins the array and returns one string containing each array element separated by commas.
See here for specifics.
3 Comments
join method also. There aren't very many other waysAmusingly, that works just by forcing it to a string type:
var someArr = ['some string here'];
console.log( "" + someArr );
And one more for the road:
someArr.forEach (function (s) { console.log(s) });
Array.prototype.pop()