---
title: "How do I check if a variable is an array, a string or null in JavaScript?"
description: "typeof works for most values but says null and arrays are both \"object\". Learn the reliable checks for arrays, strings, numbers, null, undefined and plain objects."
url: https://www.danny.engineering/answer/javascript/how-do-i-check-if-a-variable-is-an-array-a-string-or-null-in-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Javascript"]
---

# How do I check if a variable is an array, a string or null in JavaScript?

## The short answer


Use `typeof` for primitives such as strings, numbers and booleans, `Array.isArray` for arrays, and a direct comparison for `null`. Do not rely on `typeof` alone, because it returns `"object"` for both `null` and arrays.


```javascript
typeof 'hello' === 'string';  // true
Array.isArray([1, 2, 3]);     // true
value === null;               // true only for null

typeof null;  // "object"
typeof [];    // "object"
```


## Why it happens


`typeof` returns one of a small set of strings: `"undefined"`, `"boolean"`, `"number"`, `"bigint"`, `"string"`, `"symbol"`, `"function"` and `"object"`. Anything that is not a primitive or a function counts as `"object"`, so arrays, dates, maps and plain objects all look the same.


`typeof null` returning `"object"` is a bug from the first version of JavaScript. Changing it would break existing websites, so it stays. That is why `null` needs its own check.


## How to fix it


### Check primitives with typeof


For strings, numbers, booleans, bigints, symbols and `undefined`, `typeof` is reliable and works even on variables that were never declared.


```javascript
if (typeof input === 'string') {
  input.trim();
}

if (typeof count === 'number' && Number.isFinite(count)) {
  // a real number, not NaN or Infinity
}
```


`typeof NaN` is `"number"`, so add `Number.isFinite` or `Number.isNaN` when that matters.


### Check arrays with Array.isArray


`Array.isArray` is the one reliable array test. It also works for arrays created in another frame or realm, where `instanceof Array` gives the wrong answer.


```javascript
function toList(value) {
  return Array.isArray(value) ? value : [value];
}

toList('a');        // ["a"]
toList(['a', 'b']); // ["a", "b"]
```


### Check null and undefined directly


Compare with `===` for one or the other, or use `== null` to catch both.


```javascript
value === null;       // only null
value === undefined;  // only undefined
value == null;        // null or undefined

const name = user.name ?? 'Anonymous'; // default only for null or undefined
```


### Check for a plain object


To know you have an object you can read keys from, exclude `null` and arrays.


```javascript
function isPlainObject(value) {
  if (typeof value !== 'object' || value === null) return false;
  const proto = Object.getPrototypeOf(value);
  return proto === Object.prototype || proto === null;
}

isPlainObject({ a: 1 });    // true
isPlainObject([]);          // false
isPlainObject(new Date());  // false
```


### Use instanceof or a tag for built-in objects


For dates, maps, errors and class instances, `instanceof` is usually enough. `Object.prototype.toString` gives a tag string when you need something that works across realms.


```javascript
value instanceof Date;                       // true for dates
Object.prototype.toString.call(new Map());  // "[object Map]"
```


In TypeScript, wrap these checks in type guard functions so the compiler narrows the type for you after the check.


## Common pitfalls

- Writing `typeof value === 'object'` and assuming it is safe to read properties. `null` passes the check and then throws.
- Using `typeof value === 'array'`. That string never comes back from `typeof`, so the check is always false.
- Checking strings with `instanceof String`. String literals are primitives, so it is `false` for `'hello'`. Use `typeof`.
- Using `!value` to test for missing values. It also catches `0`, `''` and `false`, which are often valid. Use `== null` or `??` instead.
- Forgetting that `typeof` a function is `"function"`, not `"object"`, even though functions are objects.

## Related reading

- [MDN: typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
- [MDN: Array.isArray()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
