Location
Module to generate addresses and locations.
buildingNumber
Generates a random building number.
Available since v8.0.0
Returns: string
ts
faker.location.buildingNumber(): string
faker.location.buildingNumber() // => "5786"
faker.location.buildingNumber() // '379'
cardinalDirection
Returns a random cardinal direction (north, east, south, west).
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
useAbbr | boolean | false | If true this will return abbreviated directions (N, E, etc). Otherwise this will return the long name. |
Returns: string
ts
faker.location.cardinalDirection(useAbbr: boolean = false): string
faker.location.cardinalDirection() // => "South"
faker.location.cardinalDirection() // 'North'
faker.location.cardinalDirection(false) // 'South'
faker.location.cardinalDirection(true) // 'N'
city
Generates a random localized city name.
Available since v8.0.0
Returns: string
ts
faker.location.city(): string
faker.location.city() // => "Laruecester"
faker.location.city() // 'East Jarretmouth'
cityName
Returns a random localized and existing city name.
Available since v8.0.0
Returns: string
ts
faker.location.cityName(): string
faker.location.cityName() // => "Metairie"
faker.location.cityName() // 'San Rafael'
country
Returns a random country name.
Available since v8.0.0
Returns: string
ts
faker.location.country(): string
faker.location.country() // => "Malta"
faker.location.country() // 'Greece'
countryCode
Returns a random country code.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
alphaCode | 'alpha-2' | 'alpha-3' | 'alpha-2' | The code to return. Can be either |
Returns: string
ts
faker.location.countryCode(alphaCode: 'alpha-2' | 'alpha-3' = 'alpha-2'): string
faker.location.countryCode() // => "MA"
faker.location.countryCode() // 'SJ'
faker.location.countryCode('alpha-2') // 'GA'
faker.location.countryCode('alpha-3') // 'TJK'
county
Returns a random localized county.
Available since v8.0.0
Returns: string
ts
faker.location.county(): string
faker.location.county() // => "Borders"
faker.location.county() // 'Cambridgeshire'
direction
Returns a random direction (cardinal and ordinal; northwest, east, etc).
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
useAbbr | boolean | false | If true this will return abbreviated directions (NW, E, etc). Otherwise this will return the long name. |
Returns: string
ts
faker.location.direction(useAbbr: boolean = false): string
faker.location.direction() // => "Northeast"
faker.location.direction() // 'Northeast'
faker.location.direction(false) // 'South'
faker.location.direction(true) // 'NE'
latitude
Generates a random latitude.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
max | number | 90 | The upper bound for the latitude to generate. |
min | number | -90 | The lower bound for the latitude to generate. |
precision | number | 4 | The number of decimal points of precision for the latitude. |
Returns: number
ts
faker.location.latitude(max: number = 90, min: number = -90, precision: number = 4): number
faker.location.latitude() // => 8.7864
faker.location.latitude() // -30.9501
faker.location.latitude(10, -10, 5) // 2.68452
longitude
Generates a random longitude.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
max | number | 180 | The upper bound for the longitude to generate. |
min | number | -180 | The lower bound for the longitude to generate. |
precision | number | 4 | The number of decimal points of precision for the longitude. |
Returns: number
ts
faker.location.longitude(max: number = 180, min: number = -180, precision: number = 4): number
faker.location.longitude() // => 17.5729
faker.location.longitude() // -154.0226
faker.location.longitude(10, -10, 5) // -4.03620
nearbyGPSCoordinate
Generates a random GPS coordinate within the specified radius from the given coordinate.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
coordinate? | [latitude: number, longitude: number] | The original coordinate to get a new coordinate close to. If no coordinate is given, a random one will be chosen. | |
radius | number | 10 | The maximum distance from the given coordinate to the new coordinate. |
isMetric | boolean | false | If |
Returns: [latitude: number, longitude: number]
ts
faker.location.nearbyGPSCoordinate(coordinate?: [latitude: number, longitude: number], radius: number = 10, isMetric: boolean = false): [latitude: number, longitude: number]
faker.location.nearbyGPSCoordinate() // => [8.7864,33.4241]
faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
faker.location.nearbyGPSCoordinate([33, -170]) // [ 33.0165, -170.0636 ]
faker.location.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
ordinalDirection
Returns a random ordinal direction (northwest, southeast, etc).
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
useAbbr | boolean | false | If true this will return abbreviated directions (NW, SE, etc). Otherwise this will return the long name. |
Returns: string
ts
faker.location.ordinalDirection(useAbbr: boolean = false): string
faker.location.ordinalDirection() // => "Southeast"
faker.location.ordinalDirection() // 'Northeast'
faker.location.ordinalDirection(false) // 'Northwest'
faker.location.ordinalDirection(true) // 'NE'
secondaryAddress
Generates a random localized secondary address. This refers to a specific location at a given address such as an apartment or room number.
Available since v8.0.0
Returns: string
ts
faker.location.secondaryAddress(): string
faker.location.secondaryAddress() // => "Suite 578"
faker.location.secondaryAddress() // 'Apt. 861'
state
Returns a random localized state from this country.
Available since v8.0.0
Returns: string
ts
faker.location.state(): string
faker.location.state() // => "Nevada"
faker.location.state() // 'Georgia'
stateAbbr
Returns a random localized state's abbreviated name from this country.
Available since v8.0.0
Returns: string
ts
faker.location.stateAbbr(): string
faker.location.stateAbbr() // => "NV"
faker.location.stateAbbr() // 'ND'
street
Generates a random localized street name.
Available since v8.0.0
Returns: string
ts
faker.location.street(): string
faker.location.street() // => "Medhurst Rapids"
faker.location.street() // 'Schroeder Isle'
streetAddress
Generates a random localized street address.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
useFullAddress | boolean | false | When true this will generate a full address. Otherwise it will just generate a street address. |
Returns: string
ts
faker.location.streetAddress(useFullAddress: boolean = false): string
faker.location.streetAddress() // => "5786 Little Streets"
faker.location.streetAddress() // '0917 O'Conner Estates'
faker.location.streetAddress(false) // '34830 Erdman Hollow'
faker.location.streetAddress(true) // '3393 Ronny Way Apt. 742'
streetName
Returns a random localized street name.
Available since v8.0.0
Returns: string
ts
faker.location.streetName(): string
faker.location.streetName() // => "b"
fakerDE.location.streetName() // 'Cavill Avenue'
timeZone
Returns a random time zone.
Available since v8.0.0
Returns: string
ts
faker.location.timeZone(): string
faker.location.timeZone() // => "Africa/Harare"
faker.location.timeZone() // 'Pacific/Guam'
zipCode
Generates random zip code from specified format. If format is not specified, the locale's zip format is used.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
format? | string | The optional format used to generate the the zip code. By default, a random format is used from the locale zip formats. |
Returns: string
ts
faker.location.zipCode(format?: string): string
faker.location.zipCode() // => "57868-5846"
faker.location.zipCode() // '17839'
faker.location.zipCode('####') // '6925'
See Also
zipCodeByState
Generates random zip code from state abbreviation. If state abbreviation is not specified, a random zip code is generated according to the locale's zip format. Only works for locales with postcode_by_state definition. If a locale does not have a postcode_by_state definition, a random zip code is generated according to the locale's zip format.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
state | string | The abbreviation of the state to generate the zip code for. |
Returns: string
ts
faker.location.zipCodeByState(state: string): string
faker.location.zipCodeByState() // => "57868-5846"
fakerUS.location.zipCodeByState("AK") // '99595'
fakerUS.location.zipCodeByState("??") // '47683-9880'