{"record":{"id":"4258380497f3c403","repo":"TheAlgorithms/JavaScript","slug":"the-value-of-latitude-or-longitude-should-be-a-num","errorCode":null,"errorMessage":"The value of latitude or longitude should be a number","messagePattern":"The value of latitude or longitude should be a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Navigation/Haversine.js","lineNumber":40,"sourceCode":"  const pi = Math.PI\n  const cos1 = (latitude1 * pi) / 180.0\n  const cos2 = (latitude2 * pi) / 180.0\n  const deltaLatitude = ((latitude2 - latitude1) * pi) / 180.0\n  const deltaLongitude = ((longitude2 - longitude1) * pi) / 180.0\n\n  const alpha =\n    Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) +\n    Math.cos(cos1) *\n      Math.cos(cos2) *\n      Math.sin(deltaLongitude / 2) *\n      Math.sin(deltaLongitude / 2)\n  const constant = 2 * Math.atan2(Math.sqrt(alpha), Math.sqrt(1 - alpha))\n  return earthRadius * constant\n}\n\nconst validateLatOrLong = (value) => {\n  if (typeof value !== 'number') {\n    throw new TypeError('The value of latitude or longitude should be a number')\n  }\n}\n\nexport { haversineDistance }\n","sourceCodeStart":22,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Navigation/Haversine.js#L22-L45","documentation":"Thrown by the internal `validateLatOrLong(value)` helper used inside `haversineDistance` when a latitude or longitude argument is not `typeof === 'number'`. The guard runs per coordinate, so the offending value is whichever one was passed in. NaN passes this check (it is typeof 'number') and produces a NaN distance.","triggerScenarios":"Call `haversineDistance(lat, lon, lat2, lon2)` where any of the four is a string, null, undefined, or object. Common when geocoordinates come from JSON where they were serialised as strings.","commonSituations":"GeoJSON or REST API responses with numeric fields as strings, browser Geolocation `coords.latitude` mis-forwarded, or empty form fields passed unchanged.","solutions":["Coerce each coordinate with `Number(...)` and verify `Number.isFinite` before calling.","Reject out-of-range latitudes (>90, <-90) and longitudes (>180, <-180) upstream.","When the source is JSON, schema-validate the coordinate fields."],"exampleFix":"// before\nhaversineDistance(a.lat, a.lon, b.lat, b.lon) // a.lat could be '48.85'\n\n// after\nconst lat1 = Number(a.lat), lon1 = Number(a.lon), lat2 = Number(b.lat), lon2 = Number(b.lon)\nif (![lat1, lon1, lat2, lon2].every(Number.isFinite)) throw new TypeError('all coords must be numbers')\nhaversineDistance(lat1, lon1, lat2, lon2)","handlingStrategy":"type-guard","validationCode":"const coords = [lat1, lon1, lat2, lon2].map(Number)\nif (!coords.every((c) => typeof c === 'number' && Number.isFinite(c))) {\n  throw new TypeError('all coordinates must be finite numbers')\n}\nhaversineDistance(...coords)","typeGuard":"const isCoordinate = (x) => typeof x === 'number' && Number.isFinite(x) && Math.abs(x) <= 180","tryCatchPattern":null,"preventionTips":["Schema-validate GeoJSON / REST responses - coords are often strings.","Reject out-of-range latitudes and longitudes upstream.","Be aware NaN passes the library's guard and yields NaN distance."],"tags":["type-validation","navigation","geo","haversine"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}