Check if all elements in array are strings
You could do something like this - iterate through the array and test if everything is a string or not.
function check(arr) {
for(var i=0; i<arr.length; i++){
if(typeof arr[i] != "string") {
return false;
}
}
return true;
}
You can use Array.every
to check if all elements are strings.
const isStringsArray = arr => arr.every(i => typeof i === "string")
console.log(
isStringsArray(['a','b','c']),
isStringsArray(['a','','c']),
isStringsArray(['a', ,'c']),
isStringsArray(['a', undefined,'c']),
isStringsArray(['a','b',1])
)