turf.containsNumber
What is turf.containsNumber?
turf.containsNumber recursively walks an array (or nested array) and returns true if it contains a number at any depth. It is the low-level check Turf uses to distinguish a bare coordinate from a longer array of coordinates.
turf.containsNumber(coordinates) → booleanWhen would you use turf.containsNumber?
Use turf.containsNumber when writing your own helpers that accept "a coordinate or array of coordinates" and need to dispatch on the shape. It distinguishes [1, 2] (a position) from [[1, 2], [3, 4]] (a list of positions) without deep introspection.
It is intentionally minimal — it does not validate that the numbers represent valid lng/lat, just that something numeric exists somewhere in the array.
1turf.containsNumber([1, 2]); // true
2turf.containsNumber([[1, 2], [3, 4]]); // true (any depth)
3turf.containsNumber([]); // falseFAQs
How do I install Turf.js to use this function?
Install npm install @turf/invariant and import import { containsNumber } from '@turf/invariant'. It is also on the umbrella @turf/turf bundle.
Does it validate lng/lat ranges?
No. It only checks for the presence of a numeric element. Pair with your own range check if you need to reject invalid coordinates.
Is it faster than JSON.stringify + regex?
Yes, considerably. containsNumber does a tight typeof-check loop without allocating intermediate strings. It is suitable for hot paths.
Why would I use this instead of checking array length?
Because Turf accepts deeply nested coordinate arrays (MultiPolygon is four levels deep). containsNumber answers the higher-level question of "is there a coordinate at all here?" without caring about depth.