Here is one way you can filter out only the numbers in an array using JavaScript:
function filterNumbers(arr) {
return arr.filter(function(item) {
return typeof item === 'number';
});
}
var mixedArray = [1, 'a', 2, 'b', 3, 'c'];
var numbers = filterNumbers(mixedArray);
console.log(numbers); // Output: [1, 2, 3]
In this example, we define a function called “filterNumbers” that takes an array as an argument and returns a new array that contains only the numbers from the original array.
The function uses the Array.filter() method to create a new array with only the elements that pass the test implemented by the provided function. In this case, the test function checks if the type of the current element is “number” using the typeof operator.
We then use the function to filter out the numbers from the mixedArray and store the result in the “numbers” variable. Finally, we log the “numbers” array to the console to see the result.
This is just one way to filter out only the numbers in an array using JavaScript. There are other methods you can use, such as using a for loop and checking the type of each element manually, or using the Array.reduce() method to create a new array with only the numbers.