You can do it easily using typeof like following
For the first code block
if(typeof arr[i] === 'string'){
arr2.push(
funct1(arr[i]));
}
For the second one
if(typeof arr[i] === 'number'){
arr2.push(
funct2(arr[i]));
}
Answer from PaulShovan on Stack OverflowMDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Array
Array - JavaScript - MDN Web Docs
July 28, 2026 - Array elements are object properties in the same way that toString is a property (to be specific, however, toString() is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid: ... JavaScript syntax requires properties beginning with a digit to be accessed using bracket notation instead of dot notation.
JavaScript Array Methods in Minutes: SOME() โ 3 EXAMPLES! - YouTube
14:26
How to Work With Arrays in JavaScript - YouTube
18:54
JavaScript Arrays and Array Methods | JS for Beginners #javascript ...
01:41
JavaScript Basics - How To Find An Element In An Array - YouTube
JavaScript Array #1 - What is an array
02:25
Learn Advanced Array Searching - JavaScript Array Find (In 2 Mins) ...
Top answer 1 of 8
5
You can do it easily using typeof like following
For the first code block
if(typeof arr[i] === 'string'){
arr2.push(
funct1(arr[i]));
}
For the second one
if(typeof arr[i] === 'number'){
arr2.push(
funct2(arr[i]));
}
2 of 8
1
Create an object of functions, where each function name is the type it can handle. Iterate the array with Array#map, and select the right method from the object using typeof.
Note: I've added a boolean handler to convert the true to yes.
var arr1 = [3, "Hello", 5, "Hola", true];
var fns = {
number(item) {
return item * 2;
},
string(item) {
return item + " " + "there";
},
boolean(item) {
return item ? 'yes' : 'no'
}
};
function setNewArray(arr, fns) {
return arr.map(function(item) {
return fnstypeof item;
});
}
var result = setNewArray(arr1, fns);
console.log(result);
JavaScript Tutorial
javascripttutorial.net โบ home โบ javascript tutorial โบ javascript arrays
JavaScript Array
August 12, 2016 - In JavaScript, an array is an order list of values; each value is called an element specified by an index. An array can hold values of mixed types.
Mozilla
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Guide โบ Typed_arrays
JavaScript typed arrays - JavaScript | MDN
Typed arrays are not intended to replace arrays for any kind of functionality. Instead, they provide developers with a familiar interface for manipulating binary data. This is useful when interacting with platform features, such as audio and video manipulation, access to raw data using WebSockets, and so forth. Each entry in a JavaScript typed array is a raw binary value in one of a number of supported formats, from 8-bit integers to 64-bit floating-point numbers.
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ TypedArray
TypedArray - JavaScript - MDN Web Docs
July 28, 2026 - All typed arrays except Int8Array, Uint8Array, and Uint8ClampedArray store each element using multiple bytes. These bytes can either be ordered from most significant to least significant (big-endian) or from least significant to most significant (little-endian).
Stack Overflow
stackoverflow.com โบ questions โบ 47955379 โบ is-there-a-type-for-an-array-of-specific-type-in-javascript
Is there a type for an array of specific type in javascript? - Stack Overflow
* --------------------------------- * using 'Object(e)' so that the check works for primitives, see: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object */ return Object.getPrototypeOf(Object(e)) === type.prototype; }); }; console.log(checkArrayType([1,2,3,4], Number)); // true console.log(checkArrayType(['hello', 10], String)); // false console.log(checkArrayType([[1, 2.5], ['a', 'b']], Array)); // true console.log(checkArrayType([null, null, null, null], null)); // true console.log(checkArrayType([new String('hello'), new String('world')], Object)); // false var Foo = function Foo(){}; console.log(checkArrayType([new Foo(), new Foo()], Foo)); // true console.log(checkArrayType({a: null}, null)); // throws error console.log(checkArrayType([], 'abc')); // throws error
Mozilla
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Guide โบ Indexed_collections
Indexed collections - JavaScript - MDN Web Docs - Mozilla
2 weeks ago - The code below uses an arrow function to return the type of each array element (this uses object destructuring syntax for function arguments to unpack the type element from the passed object). The result is an object that has properties named after the unique strings returned by the callback.
W3schools
w3schools.dev โบ js โบ js_arrays.asp
JavaScript Arrays - W3Schools
The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements".
GeeksforGeeks
geeksforgeeks.org โบ javascript-arrays
JavaScript Arrays | GeeksforGeeks
In JavaScript, an array is an ordered list of values. Each value is called an element, and each element has a numeric position in the array, known as its index. Arrays in JavaScript are zero-indexed, meaning the first element is at index 0, ...
Published: February 10, 2025
Code Institute
codeinstitute.net โบ blog โบ javascript โบ javascript arrays
Javascript Arrays: A Guide - Code Institute Global
December 20, 2022 - Since JavaScript arrays do not refer to associative arrays, it is impossible to access array items using random strings as indexes; instead, nonnegative numbers (or their corresponding string forms) must be used. Square brackets are used to indicate how to obtain an arrayโs elements.
Wikibooks
en.wikibooks.org โบ wiki โบ JavaScript โบ Arrays
JavaScript/Arrays - Wikibooks, open books for an open world
December 20, 2007 - (But there are also typed arrays where all values have the same data type.) When you create an array, you do not need to declare a size - but you can. Arrays grow automatically. This makes arrays very convenient to use, but not well-suited for applications in numerical analysis. Because arrays are objects, they have methods and properties you can invoke at will. For example, the .length property indicates how many elements are currently in the array.