Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
javascript expression to check if array includes only a specified type

Hello everyone I struggling to create an expression to check if a specific array includes only a specified type values.Here is what I tried :

const isArrayOfType = (arr, type) =>
  arr.forEach((item) => typeof item == type) ? true : false;

const arr = [1, 2, 3];
const typ = 'number';

console.log(isArrayOfType(arr, typ));

The main goal is to not use return in the expression.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

forEach doesnt return anything, I think you're looking for every (and the ternary operator is unecessary as it already returns a boolean)

const isArrayOfType = (arr, type) =>
  arr.every((item) => typeof item == type);

const arr = [1, 2, 3];
const typ = 'number';

console.log(isArrayOfType(arr, typ));
console.log(isArrayOfType(["one",2,3], typ));

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!