Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zod · Module

z.nan

z.nan() creates a schema that accepts JavaScript’s NaN value and rejects all other inputs. Use it when NaN must be validated explicitly rather than treated as an ordinary number.

How it works

z.nan() creates a Zod schema dedicated to validating JavaScript's NaN value. Parsing NaN succeeds, while parsing any other value fails with a validation error.

Syntax

z.nan()

Returns

A Zod schema that accepts NaN and rejects other values.

Key points

  • Choose z.nan() when a value must specifically be JavaScript NaN.
  • Parsing the NaN primitive with the schema succeeds.
  • Parsing a value other than NaN fails validation.
  • The general-purpose alternative for numeric values is z.number(), not z.nan().

Common mistakes

  • z.number() does not accept NaN; use z.nan() when NaN is the intended value.
  • The schema is exclusive to NaN and rejects non-NaN values, including strings.

Example: Validate NaN

import { z } from "zod";

const schema = z.nan();

console.log(schema.parse(NaN)); // NaN
schema.parse("anything else"); // throws ZodError

Creates a schema that accepts JavaScript NaN and rejects other values.

When to use it

Special numeric sentinel

Validate a computed numeric result that may intentionally be NaN.

Input validation

Guard an API or function boundary where NaN is an explicitly permitted value.

Union schemas

Use NaN as one branch of a broader numeric schema.

Interview questions

QWhat does z.nan() validate?

Use z.nan() to define a schema for JavaScript's NaN value. Calling parse(NaN) succeeds, while parsing another value fails.

Follow-up: How would you validate ordinary numeric input instead? Use z.number(), which accepts finite numbers but rejects NaN and Infinity.

QWhat is the syntax for creating a NaN schema?

No arguments are required: the declaration is z.nan(). It returns a Zod schema whose parse operation accepts NaN.

Follow-up: What should be used if the input may be either NaN or a regular number? Combine schemas with z.union([z.number(), z.nan()]).

QWhy not use z.number() to accept NaN?

z.number() accepts finite numbers, but NaN is not finite and is rejected by that schema. z.nan() exists for the special case where NaN must be accepted.

Follow-up: What happens with Infinity? z.number() also rejects Infinity, and z.nan() rejects it because it only accepts NaN.

  • z.number()
  • parse()
  • safeParse()
  • z.union()
  • z.infer

Source: Defining schemas - Zod. Checked 2026-10-15.