Skip to content
On this page

Datatype

Module to generate various primitive values and data types.

array

Returns an array with random strings and numbers.

Available since v5.5.0

Parameters

NameTypeDefaultDescription
lengthnumber10

Size of the returned array.

Returns: number | string[]

ts
faker.datatype.array(length: number = 10): (string | number)[]
faker.datatype.array() // => ["XcoYpSoHZ]",43758,89177,96366,38344,79172,"RM...
faker.datatype.array() // [ 94099, 85352, 'Hz%T.C\\l;8', '|#gmtw3otS', '2>:rJ|3$&d', 56864, 'Ss2-p0RXSI', 51084, 2039, 'mNEU[.r0Vf' ]
faker.datatype.array(3) // [ 61845, 'SK7H$W3:d*', 'm[%7N8*GVK' ]

bigInt

Returns a BigInt number.

Available since v6.0.0

Parameters

NameTypeDefaultDescription
options?bigint | boolean | number | string | { ... }

Maximum value or options object.

options.max?bigint | boolean | number | stringmin + 999999999999999n

Upper bound for generated bigint.

options.min?bigint | boolean | number | string0n

Lower bound for generated bigint.

Returns: bigint

ts
faker.datatype.bigInt(options?: bigint | boolean | number | string | {
  max: bigint | boolean | number | string,
  min: bigint | boolean | number | string
}): bigint
faker.datatype.bigInt() // 55422n
faker.datatype.bigInt(100n) // 52n
faker.datatype.bigInt({ min: 1000000n }) // 431433n
faker.datatype.bigInt({ max: 100n }) // 42n
faker.datatype.bigInt({ min: 10n, max: 100n }) // 36n

boolean

Returns the boolean value true or false.

Available since v5.5.0

Returns: boolean

ts
faker.datatype.boolean(): boolean
faker.datatype.boolean() // => true
faker.datatype.boolean() // false

datetime

Returns a Date object using a random number of milliseconds since the Unix Epoch (1 January 1970 UTC).

Available since v5.5.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Max number of milliseconds since unix epoch or options object.

options.max?number4102444800000

Upper bound for milliseconds since base date. When not provided or larger than 8640000000000000, 2100-01-01 is considered as maximum generated date.

options.min?number631152000000

Lower bound for milliseconds since base date. When not provided or smaller than -8640000000000000, 1990-01-01 is considered as minimum generated date.

Returns: Date

ts
faker.datatype.datetime(options: number | {
  max: number,
  min: number
} = {}): Date
faker.datatype.datetime() // => "2050-05-15T16:19:19.092Z"
faker.datatype.datetime() // '2089-04-17T18:03:24.956Z'
faker.datatype.datetime(1893456000000) // '2022-03-28T07:00:56.876Z'
faker.datatype.datetime({ min: 1577836800000, max: 1893456000000 }) // '2021-09-12T07:13:00.255Z'

float

Returns a single random floating-point number for the given precision or range and precision.

Available since v5.5.0

Parameters

NameTypeDefaultDescription
options?number | { ... }

Precision or options object.

options.max?number99999

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.precision?number0.01

Precision of the generated number.

Returns: number

ts
faker.datatype.float(options?: number | {
  max: number,
  min: number,
  precision: number
}): number
faker.datatype.float() // => 54880.8
faker.datatype.float() // 51696.36
faker.datatype.float(0.1) // 52023.2
faker.datatype.float({ min: 1000000 }) // 212859.76
faker.datatype.float({ max: 100 }) // 28.11
faker.datatype.float({ precision: 0.1 }) // 84055.3
faker.datatype.float({ min: 10, max: 100, precision: 0.001 }) // 57.315

hexadecimal

Returns a hexadecimal number.

Available since v6.1.2

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

The optional options object.

options.case?'lower' | 'mixed' | 'upper''mixed'

Case of the generated number.

options.length?number1

Length of the generated number.

options.prefix?string'0x'

Prefix for the generated number.

Returns: string

ts
faker.datatype.hexadecimal(options: number | {
  case: 'lower' | 'mixed' | 'upper',
  length: number,
  prefix: string
} = {}): string
faker.datatype.hexadecimal() // => "0xc"
faker.datatype.hexadecimal() // '0xB'
faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB'
faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE'
faker.datatype.hexadecimal({ case: 'lower' }) // '0xf'
faker.datatype.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1'
faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB'
faker.datatype.hexadecimal({ prefix: '', case: 'lower' }) // 'd'
faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'

json

Returns a string representing JSON object with 7 pre-defined properties.

Available since v5.5.0

Returns: string

ts
faker.datatype.json(): string
faker.datatype.json() // => "{\"foo\":\"XcoYpSoHZ]\",\"bar\":43758,\"bike\"...
faker.datatype.json() // `{"foo":"mxz.v8ISij","bar":29154,"bike":8658,"a":"GxTlw$nuC:","b":40693,"name":"%'<FTou{7X","prop":"X(bd4iT>77"}`

number

Returns a single random number between zero and the given max value or the given range with the specified precision. The bounds are inclusive.

Available since v5.5.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }99999

Maximum value or options object.

options.max?numbermin + 99999

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.precision?number1

Precision of the generated number.

Returns: number

ts
faker.datatype.number(options: number | {
  max: number,
  min: number,
  precision: number
} = 99999): number
faker.datatype.number() // => 54881
faker.datatype.number() // 55422
faker.datatype.number(100) // 52
faker.datatype.number({ min: 1000000 }) // 1031433
faker.datatype.number({ max: 100 }) // 42
faker.datatype.number({ precision: 0.01 }) // 64246.18
faker.datatype.number({ min: 10, max: 100, precision: 0.01 }) // 36.94

string

Returns a string containing UTF-16 chars between 33 and 125 (! to }).

Available since v5.5.0

Parameters

NameTypeDefaultDescription
lengthnumber10

Length of the generated string. Max length is 2^20.

Returns: string

ts
faker.datatype.string(length: number = 10): string
faker.datatype.string() // => "TXcoYpSoHZ"
faker.datatype.string() // 'Zo!.:*e>wR'
faker.datatype.string(5) // '6Bye8'

uuid

Returns a UUID v4 (Universally Unique Identifier).

Available since v5.5.0

Returns: string

ts
faker.datatype.uuid(): string
faker.datatype.uuid() // => "89bd9d8d-69a6-474e-8f46-7cc8796ed151"
faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'

Released under the MIT License.