Skip to content

Datatype

Module to generate various primitive values and data types.

Overview

Most of the methods in this module are deprecated and have been moved to other modules like faker.number and faker.string, see individual entries for replacements.

For a simple random true or false value, use boolean().

array

Deprecated

This method is deprecated and will be removed in a future version.

Use your own function to build complex arrays.

Returns an array with random strings and numbers.

Available since v5.5.0

Parameters

NameTypeDefaultDescription
lengthnumber | { ... }10

Size of the returned array.

length.maxnumber

The maximum size of the array.

length.minnumber

The minimum size of the array.

Returns: Array<number | string>

ts
faker.datatype.array(length: number | {
  max: number,
  min: number
} = 10): (string | number)[]
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' ]
faker.datatype.array({ min: 3, max: 5 }) // [ 99403, 76924, 42281, "Q'|$&y\\G/9" ]

bigInt

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.number.bigInt() instead.

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

Throws: When options define max < min.

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.

Note: A probability of 0.75 results in true being returned 75% of the calls; likewise 0.3 => 30%. If the probability is <= 0.0, it will always return false. If the probability is >= 1.0, it will always return true. The probability is limited to two decimal places.

Available since v5.5.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

The optional options object or the probability ([0.00, 1.00]) of returning true.

options.probability?number0.5

The probability ([0.00, 1.00]) of returning true.

Returns: boolean

ts
faker.datatype.boolean(options: number | {
  probability: number
} = {}): boolean
faker.datatype.boolean() // false
faker.datatype.boolean(0.9) // true
faker.datatype.boolean({ probability: 0.1 }) // false

datetime

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.date.between({ from: min, to: max }) or faker.date.anytime() instead.

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() // '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

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.number.float() instead.

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

Available since v5.5.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Precision or options object.

options.max?numbermin + 99999

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.precision?number0.01

Precision of the generated number.

Returns: number

Throws: When min is greater than max. When precision is negative.

ts
faker.datatype.float(options: number | {
  max: number,
  min: number,
  precision: number
} = {}): number
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

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.string.hexadecimal() or faker.number.hex() instead.

Returns a hexadecimal number.

Available since v6.1.2

Parameters

NameTypeDefaultDescription
options{ ... }{}

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: {
  case: 'lower' | 'mixed' | 'upper',
  length: number,
  prefix: string
} = {}): string
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

Deprecated

This method is deprecated and will be removed in a future version.

Build your own function to generate complex objects.

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":"mxz.v8ISij","bar":29154,"bike":8658,"a":"GxTlw$nuC:","b":40693,"name":"%'<FTou{7X","prop":"X(bd4iT>77"}`

number

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.number.int() or faker.number.float() instead.

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

Throws: When min is greater than max. When precision is negative.

ts
faker.datatype.number(options: number | {
  max: number,
  min: number,
  precision: number
} = 99999): number
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

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.string.sample() instead.

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

Available since v5.5.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Length of the generated string or an options object.

options.length?number10

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

Returns: string

ts
faker.datatype.string(options: number | {
  length: number
} = {}): string
faker.datatype.string() // 'Zo!.:*e>wR'
faker.datatype.string(5) // '6Bye8'
faker.datatype.string({ length: 7 }) // 'dzOT00e'

uuid

Deprecated

This method is deprecated and will be removed in a future version.

Use faker.string.uuid() instead.

Returns a UUID v4 (Universally Unique Identifier).

Available since v5.5.0

Returns: string

ts
faker.datatype.uuid(): string
faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'

Released under the MIT License.