noop()
noop():
void
A function that does nothing and returns undefined.
Useful as a default callback or placeholder.
Returns: voidโ
Always returns undefined.
Sinceโ
2.0.0
Also known asโ
always (Ramda) ยท constVoid (Effect) ยท doNothing (Remeda) ยท noop (Lodash, es-toolkit, Radashi, Antfu) ยท โ (Modern Dash)
Exampleโ
// Default callback to avoid null checks
function fetchData(onSuccess: () => void = noop) {
// No need for: if (onSuccess) onSuccess()
onSuccess();
}
// In tests
const handler: Arrayable<() => void> = [noop, noop];
expect(processItems(handler)).toHaveLength(2);
How it works?โ
A function that does nothing and returns undefined.
Use Case: Default Callbackโ
Use Casesโ
Provide default callbacks ๐โ
Use as a placeholder for optional callback functions to avoid null checks. Essential for cleaner code and avoiding "if (callback)" checks.
function fetchData(onSuccess = noop) {
// Safe to call even if not provided
onSuccess();
}
fetchData(); // No error
Stub functions for testing ๐โ
Use as a placeholder for dependencies during unit testing. Useful for mocking functions that don't need to return values.
const mockLogger = {
info: noop,
warn: noop,
error: noop,
};
// Test logic without filling console with logs
processData(data, mockLogger);
Disable event handlers temporarilyโ
Replace active handlers with noop to "mute" functionality without removing listeners. Useful for feature flags or temporary disabling during maintenance.
let onNotification = showToast;
// Mute notifications during focus mode
function enableFocusMode() {
onNotification = noop;
}
function disableFocusMode() {
onNotification = showToast;
}
// Code doesn't need to check if handler exists
socket.on("notification", (msg) => onNotification(msg));