Language concepts

Guard clause

Try the free JavaScript compiler

An early conditional exit that handles an invalid or boundary case before the main path. It is a programming pattern, not a separate JavaScript statement.

Formal Syntax & Structure

javascript syntaxjavascript
if (invalid) { return fallback; }

Working Example

Example Snippetjavascript
function f(n) { if (n < 0) { return "invalid"; } return n; } console.log(f(-1));
JavaScript Reference