Как проверить является ли переменная массивом js — Q&A Хекслет
2026-02-26 20:20 Diff

Ответы

Для того, чтобы проверить является ли переменная массивом, можно воспользоваться встроенным методом Array.isArray().

Предлагаю воспользоваться оператором instanceof:

Документация:

Предлагаю на рассмотрение такой метод:

const arr = [1,2,3,4,5]; const arrEmpty = []; const text = 'Text here'; const object = { name: 'Max', age: 30 }; const number = 5; console.log(typeof arr === 'object' && arr !== null && arr.length >= 0) // => true console.log(typeof arrEmpty === 'object' && arrEmpty !== null && arrEmpty.length >= 0) // => true console.log(typeof text === 'object' && text !== null && text.length >= 0) // => false console.log(typeof object === 'object' && object !== null && object.length >= 0) // => false console.log(typeof number === 'object' && number !== null && number.length >= 0) // => false