trekhleb/javascript-algorithms · error · Error
Graph vertex must have a value
Error message
Graph vertex must have a value
What it means
GraphVertex's constructor requires a value (src/data-structures/graph/GraphVertex.js:8-10) because the value doubles as the vertex key - getKey() returns this.value verbatim - and every graph lookup depends on that key existing. Only undefined is rejected: null, 0, '' and false are all accepted as legitimate values.
Source
Thrown at src/data-structures/graph/GraphVertex.js:9
import LinkedList from '../linked-list/LinkedList';
export default class GraphVertex {
/**
* @param {*} value
*/
constructor(value) {
if (value === undefined) {
throw new Error('Graph vertex must have a value');
}
/**
* @param {GraphEdge} edgeA
* @param {GraphEdge} edgeB
*/
const edgeComparator = (edgeA, edgeB) => {
if (edgeA.getKey() === edgeB.getKey()) {
return 0;
}
return edgeA.getKey() < edgeB.getKey() ? -1 : 1;
};
// Normally you would store string value like vertex name.
// But generally it may be any object as well
this.value = value;
this.edges = new LinkedList(edgeComparator);View on GitHub (pinned to 85293e3e2b)
Solutions
- Supply a concrete value or default: new GraphVertex(row.label ?? `vertex-${row.id}`) - only undefined throws, so null and falsy primitives are fine when intentional.
- Validate records at the boundary and fail with your own clearer error before reaching the constructor.
- If 'no value' is meaningful in your domain, pass null explicitly rather than leaving the argument out.
Example fix
// before
const vertex = new GraphVertex(row.label); // row.label === undefined -> Error
// after
const vertex = new GraphVertex(row.label ?? `vertex-${row.id}`); Defensive patterns
Strategy: type-guard
Type guard
// undefined is the ONLY rejected value - null, 0, '' and false are valid
const hasVertexValue = (value) => value !== undefined;
if (hasVertexValue(row.label)) {
graph.addVertex(new GraphVertex(row.label));
} else {
// record is malformed: report it before constructing
} Try / catch
try {
vertex = new GraphVertex(candidate);
} catch (error) {
if (error.message === 'Graph vertex must have a value') {
vertex = new GraphVertex(fallbackValue);
} else {
throw error;
}
} Prevention
- Give the constructor call a fallback (?? default) whenever the input is optional.
- Validate parsed rows for required fields before graph construction.
- Remember null is legal - do not add truthiness checks that reject 0 or ''.
When it happens
Trigger: new GraphVertex() with no argument; new GraphVertex(obj.missingProperty) where parsing or destructuring produced undefined; new GraphVertex(row.name) where some records lack the field; a helper with an optional parameter that forwards undefined into the constructor.
Common situations: Building vertices from JSON/CSV rows where a column is missing or null-vs-absent varies between records; refactors that rename the field feeding the constructor; optional function parameters that silently propagate undefined.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/01c8893a27ae7ffb.
Report an issue: GitHub.