Core data generation utilities including numbers, strings, booleans, and helper functions for arrays and objects. These modules provide the foundational building blocks for generating primitive data types and manipulating data structures.
Generate various types of numbers with range, precision, and format constraints.
/**
* Generate a random integer within optional bounds
* @param options - Configuration options or max value
* @returns Random integer
*/
int(options?: number | {
/** Lower bound for generated number */
min?: number;
/** Upper bound for generated number */
max?: number;
/** Generated number will be a multiple of this value */
multipleOf?: number;
}): number;
/**
* Generate a random floating-point number
* @param options - Configuration options or max value
* @returns Random float
*/
float(options?: number | {
/** Lower bound for generated number, inclusive */
min?: number;
/** Upper bound for generated number, exclusive */
max?: number;
/** The maximum number of digits after decimal point */
fractionDigits?: number;
/** Generated number will be a multiple of this value */
multipleOf?: number;
}): number;
/**
* Generate a random BigInt for very large integers
* @param options - Configuration options
* @returns Random BigInt
*/
bigInt(options?: {
/** Lower bound for generated number */
min?: bigint;
/** Upper bound for generated number */
max?: bigint;
}): bigint;Usage Examples:
import { faker } from "@faker-js/faker";
// Generate basic integers
const randomInt = faker.number.int();
// Example: 2900970162509863
const boundedInt = faker.number.int({ min: 10, max: 100 });
// Example: 57
const multipleInt = faker.number.int({ min: 10, max: 100, multipleOf: 5 });
// Example: 45
// Generate floating-point numbers
const randomFloat = faker.number.float();
// Example: 0.5688541042618454
const boundedFloat = faker.number.float({ min: 20, max: 30 });
// Example: 23.94764115102589
const precisionFloat = faker.number.float({
min: 10,
max: 100,
fractionDigits: 2
});
// Example: 65.72
// Generate large integers
const bigNumber = faker.number.bigInt({
min: 1000000000000000000n,
max: 9999999999999999999n
});
// Example: 5432109876543210987nGenerate numbers in different bases with custom formatting options.
/**
* Generate a hexadecimal number string
* @param options - Configuration options
* @returns Hexadecimal string
*/
hex(options?: {
/** Lower bound for generated number */
min?: number;
/** Upper bound for generated number */
max?: number;
}): string;
/**
* Generate an octal number string
* @param options - Configuration options
* @returns Octal string
*/
octal(options?: {
/** Lower bound for generated number */
min?: number;
/** Upper bound for generated number */
max?: number;
}): string;
/**
* Generate a binary number string
* @param options - Configuration options
* @returns Binary string
*/
binary(options?: {
/** Lower bound for generated number */
min?: number;
/** Upper bound for generated number */
max?: number;
}): string;Generate strings from various character sets with flexible length and casing options.
/**
* Generate a string from custom characters
* @param characters - Character set to use
* @param length - String length or range
* @returns Generated string
*/
fromCharacters(
characters: string | ReadonlyArray<string>,
length?: number | {
/** Minimum length */
min: number;
/** Maximum length */
max: number;
}
): string;
/**
* Generate alphabetic characters (A-Z)
* @param options - Configuration options or length
* @returns Alphabetic string
*/
alpha(options?: number | {
/** String length or range */
length?: number | { min: number; max: number };
/** Character casing */
casing?: 'upper' | 'lower' | 'mixed';
/** Characters to exclude */
exclude?: string[];
}): string;
/**
* Generate alphanumeric characters (A-Z, 0-9)
* @param options - Configuration options or length
* @returns Alphanumeric string
*/
alphanumeric(options?: number | {
/** String length or range */
length?: number | { min: number; max: number };
/** Character casing */
casing?: 'upper' | 'lower' | 'mixed';
/** Characters to exclude */
exclude?: string[];
}): string;
/**
* Generate numeric string (0-9)
* @param options - Configuration options or length
* @returns Numeric string
*/
numeric(options?: number | {
/** String length or range */
length?: number | { min: number; max: number };
/** Whether leading zeros are allowed */
allowLeadingZeros?: boolean;
/** Digits to exclude */
exclude?: string[];
}): string;Usage Examples:
import { faker } from "@faker-js/faker";
// Custom character sets
const customString = faker.string.fromCharacters('abc123', 10);
// Example: 'a1b3c2a1b3'
const customArray = faker.string.fromCharacters(['☆', '★', '✦'], 5);
// Example: '☆★✦☆★'
// Alphabetic strings
const mixedAlpha = faker.string.alpha(10);
// Example: 'fEcAaCVbaR'
const lowerAlpha = faker.string.alpha({
length: 8,
casing: 'lower'
});
// Example: 'hvnkqpwr'
const excludedAlpha = faker.string.alpha({
length: 5,
exclude: ['a', 'e', 'i', 'o', 'u']
});
// Example: 'BCDFY'
// Alphanumeric strings
const alphaNum = faker.string.alphanumeric(15);
// Example: 'Yc3d2o7cktD84oZ'
// Numeric strings
const numericString = faker.string.numeric(10);
// Example: '6843579468'
const nonZeroStart = faker.string.numeric({
length: 8,
allowLeadingZeros: false
});
// Example: '72564846'Generate strings in different numerical bases with prefix support.
/**
* Generate a binary string with optional prefix
* @param options - Configuration options
* @returns Binary string
*/
binary(options?: {
/** String length or range (excluding prefix) */
length?: number | { min: number; max: number };
/** Prefix for the generated string */
prefix?: string;
}): string;
/**
* Generate an octal string with optional prefix
* @param options - Configuration options
* @returns Octal string
*/
octal(options?: {
/** String length or range (excluding prefix) */
length?: number | { min: number; max: number };
/** Prefix for the generated string */
prefix?: string;
}): string;
/**
* Generate a hexadecimal string with optional prefix
* @param options - Configuration options
* @returns Hexadecimal string
*/
hexadecimal(options?: {
/** String length or range (excluding prefix) */
length?: number | { min: number; max: number };
/** Character casing */
casing?: 'upper' | 'lower' | 'mixed';
/** Prefix for the generated string */
prefix?: string;
}): string;Usage Examples:
import { faker } from "@faker-js/faker";
// Binary strings
const binaryString = faker.string.binary();
// Example: '0b1'
const longBinary = faker.string.binary({ length: 8 });
// Example: '0b10110101'
const customBinary = faker.string.binary({
length: 4,
prefix: 'bin_'
});
// Example: 'bin_1010'
// Octal strings
const octalString = faker.string.octal();
// Example: '0o3'
const longOctal = faker.string.octal({ length: 6 });
// Example: '0o152621'
// Hexadecimal strings
const hexString = faker.string.hexadecimal();
// Example: '0xB'
const upperHex = faker.string.hexadecimal({
length: 8,
casing: 'upper'
});
// Example: '0xE3F38014'
const colorHex = faker.string.hexadecimal({
length: 6,
casing: 'lower',
prefix: '#'
});
// Example: '#f12a97'Generate standard identifier formats including UUIDs and NanoIDs.
/**
* Generate a UUID v4 (Universally Unique Identifier)
* @returns Standard UUID v4 string
*/
uuid(): string;
/**
* Generate a NanoID with optional customization
* @param options - Configuration options
* @returns NanoID string
*/
nanoid(options?: {
/** Minimum length */
min?: number;
/** Maximum length */
max?: number;
}): string;
/**
* Generate a ULID (Universally Unique Lexicographically Sortable Identifier)
* @param options - Configuration options
* @returns ULID string
*/
ulid(options?: {
/** Reference date for timestamp component */
refDate?: string | Date | number;
}): string;Usage Examples:
import { faker } from "@faker-js/faker";
// Generate UUIDs
const uuid = faker.string.uuid();
// Example: '4136cd0b-d90b-4af7-b485-5d1ded8db252'
// Generate NanoIDs
const nanoId = faker.string.nanoid();
// Example: 'Uakgb_J5m9g~0JDMbcJqLJ'
const customNanoId = faker.string.nanoid({ min: 8, max: 12 });
// Example: 'x1f3k9m2p'
// Generate ULIDs (time-sortable)
const ulid = faker.string.ulid();
// Example: '01ARZ3NDEKTSV4RRFFQ69G5FAV'
const timedUlid = faker.string.ulid({
refDate: '2023-01-01T00:00:00.000Z'
});
// Example: '01GNFX5RMV8QR5Y3Y4T6K2Q8V1'Generate boolean values with configurable probability.
/**
* Generate a random boolean value
* @param options - Configuration options
* @returns Random boolean
*/
boolean(options?: {
/** Probability of returning true (0-1) */
probability?: number;
}): boolean;Usage Examples:
import { faker } from "@faker-js/faker";
// Generate random boolean
const randomBool = faker.datatype.boolean();
// Example: true or false (50/50 chance)
// Generate biased boolean
const biasedBool = faker.datatype.boolean({ probability: 0.8 });
// Example: true (80% chance) or false (20% chance)
const rarelytrue = faker.datatype.boolean({ probability: 0.1 });
// Example: false (90% chance) or true (10% chance)Utility functions for working with arrays, objects, and data manipulation.
/**
* Get a random element from an array
* @param array - Array to select from
* @returns Random array element
*/
arrayElement<T>(array: ReadonlyArray<T>): T;
/**
* Get multiple random elements from an array
* @param array - Array to select from
* @param count - Number of elements to select
* @returns Array of selected elements
*/
arrayElements<T>(array: ReadonlyArray<T>, count?: number): T[];
/**
* Shuffle an array randomly
* @param array - Array to shuffle
* @param options - Shuffle options
* @returns Shuffled array
*/
shuffle<T>(array: T[], options?: { inplace?: boolean }): T[];
/**
* Generate multiple instances using a function
* @param method - Function to call repeatedly
* @param options - Generation options
* @returns Array of results
*/
multiple<T>(
method: () => T,
options?: {
/** Number of times to call the method */
count?: number | { min: number; max: number };
}
): T[];Usage Examples:
import { faker } from "@faker-js/faker";
// Array manipulation
const colors = ['red', 'green', 'blue', 'yellow'];
const randomColor = faker.helpers.arrayElement(colors);
// Example: 'blue'
const someColors = faker.helpers.arrayElements(colors, 2);
// Example: ['red', 'yellow']
const shuffledColors = faker.helpers.shuffle([...colors]);
// Example: ['yellow', 'red', 'blue', 'green']
// Multiple generation
const names = faker.helpers.multiple(
() => faker.person.firstName(),
{ count: { min: 3, max: 6 } }
);
// Example: ['Alice', 'Bob', 'Charlie', 'Diana']
const numbers = faker.helpers.multiple(
() => faker.number.int({ min: 1, max: 100 }),
{ count: 5 }
);
// Example: [23, 67, 12, 89, 45]type Casing = 'upper' | 'lower' | 'mixed';
type LowerAlphaChar = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
type UpperAlphaChar = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
type NumericChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type AlphaChar = LowerAlphaChar | UpperAlphaChar;
type AlphaNumericChar = AlphaChar | NumericChar;The data type modules throw FakerError for invalid configurations:
multipleOf must be a positive numberfractionDigits cannot be used with multipleOfmin must be less than or equal to maxfaker.helpers.multiple() for efficiency