Just started with Javascript and am trying to check the type of an element in an array: I have a function that takes two functions as parameters. I have an array which has a combination of string and number elements. The idea is to trigger functions based on the type of element in the sample array and then push a new element to a new array. I'm not sure which bulit in function i can use here. I tried indexOf and typeof but these don't seem to serve the purpose or maybe I'm doing this wrong. Below is the code. Thanks a lot!
var arr1 = [3, "Hello", 5, "Hola"];
function setNewArray(arr, funct1, funct2){
var arr2 = [];
for(var i = 0; i < arr.length; i++){
if(/*check if arr[i] is a string*/){
arr2.push(funct1(arr[i]));
}
if(/*check if arr[i] is a number*/){
arr2.push(funct2(arr[i]));
}
}
return arr2;
}
var numfunct = function(item){
return item * 2;
}
var strfunct = function(item){
return item + " " + "there";
}
var result = setNewArray(arr1, numfunct, strfunct);
console.log(result);

typeofis what you need, how did you used it?