Automation Blog

if/else and switch Statements in Zapier JavaScript Code Steps

How to apply conditional logic inside a Zapier JavaScript Code step — using if/else for boolean branching, switch statements for multi-value routing, and lookup objects for clean value mapping — with practical examples for common automation scenarios.

ZapierJavaScriptCode Steps

By Troy Tessalone · · 5 minutes

Automation Guide

A practical field guide from Automation Ace.

if/else and switch Statements in Zapier JavaScript Code Steps

Zapier's Filter by Zapier step handles simple "continue if X equals Y" checks, but conditional logic inside a Code step is far more powerful: you can set output values differently depending on multiple conditions, route to different logic branches based on a field value, map a set of input values to output values, and combine conditions with AND/OR logic. This guide covers the conditional patterns that come up most in Zapier automation Code steps.

Basic if/else

const status = inputData.status; // e.g. "active", "inactive", "pending"

let label, shouldNotify;

if (status === 'active') {
  label = 'Active Customer';
  shouldNotify = 'true';
} else if (status === 'pending') {
  label = 'Pending Review';
  shouldNotify = 'true';
} else {
  label = 'Inactive';
  shouldNotify = 'false';
}

output = { label, should_notify: shouldNotify };

Always assign output in every branch. A common bug is assigning output inside only the if block — if the condition doesn't match, Zapier reports "output is not defined."

if/else with Multiple Conditions (AND / OR)

const score = parseInt(inputData.score, 10) || 0;
const tier = inputData.tier; // 'gold', 'silver', 'bronze'
const isVerified = inputData.is_verified === 'true';

let priority;

if (score >= 90 && tier === 'gold') {
  priority = 'high';
} else if (score >= 70 || tier === 'gold') {
  priority = 'medium';
} else if (!isVerified) {
  priority = 'low-unverified';
} else {
  priority = 'low';
}

output = { priority };

Ternary Operator for Single-Value Conditionals

For simple true/false assignments, the ternary operator is more concise than if/else:

const amount = parseFloat(inputData.amount) || 0;

const tier = amount >= 1000 ? 'enterprise' : amount >= 100 ? 'pro' : 'free';
const isHighValue = amount >= 500 ? 'true' : 'false';
const label = inputData.name ? inputData.name.trim() : 'Unknown';

output = { tier, is_high_value: isHighValue, label };

switch Statement for Multi-Value Routing

A switch statement is cleaner than a long if/else chain when matching one variable against many possible exact values:

const country = inputData.country_code.toUpperCase(); // e.g. 'US', 'GB', 'DE'

let currency, timezone, language;

switch (country) {
  case 'US':
    currency = 'USD';
    timezone = 'America/New_York';
    language = 'en';
    break;
  case 'GB':
    currency = 'GBP';
    timezone = 'Europe/London';
    language = 'en';
    break;
  case 'DE':
  case 'AT':
  case 'CH':
    currency = 'EUR';
    timezone = 'Europe/Berlin';
    language = 'de';
    break;
  default:
    currency = 'USD';
    timezone = 'UTC';
    language = 'en';
}

output = { currency, timezone, language };

The break statement is required at the end of each case to prevent "fall-through" (executing the next case's code). Note that case 'DE' and case 'AT' and case 'CH' all share the same outcome by intentionally omitting break until the last one — this is valid fall-through used deliberately.

Lookup Object as a switch Alternative

For simple value-to-value mapping, a plain object often replaces a switch statement more cleanly:

const statusMap = {
  'new':      { label: 'New Lead',       color: '#2196F3' },
  'active':   { label: 'Active',         color: '#4CAF50' },
  'churned':  { label: 'Churned',        color: '#F44336' },
  'paused':   { label: 'Paused',         color: '#FF9800' }
};

const status = inputData.status;
const mapped = statusMap[status] || { label: 'Unknown', color: '#9E9E9E' };

output = {
  status_label: mapped.label,
  status_color: mapped.color
};

Lookup objects are easier to extend — adding a new status means adding one line, not a new case block.

Conditional Output Fields

A Code step's output object can be built conditionally — include or exclude fields based on logic:

const type = inputData.record_type;
const baseOutput = {
  id: inputData.id,
  type: type,
  created_at: inputData.created_at
};

// Add type-specific fields conditionally
if (type === 'invoice') {
  baseOutput.amount = inputData.amount;
  baseOutput.due_date = inputData.due_date;
  baseOutput.client_name = inputData.client_name;
} else if (type === 'subscription') {
  baseOutput.plan = inputData.plan;
  baseOutput.renewal_date = inputData.renewal_date;
}

// Always include a status flag
baseOutput.processed = 'true';

output = baseOutput;

Nested Conditions and Early Returns

For complex branching, assigning output early and using a pattern to exit prevents deeply nested if/else trees:

// Validate inputs first — exit early on invalid data
if (!inputData.email || !inputData.email.includes('@')) {
  output = { valid: 'false', error: 'Invalid email', result: '' };
  return; // In Zapier JS Code steps, return exits execution
}

if (!inputData.record_id) {
  output = { valid: 'false', error: 'Missing record ID', result: '' };
  return;
}

// Main logic — only reached if validations pass
const result = inputData.email.toLowerCase().trim();
output = { valid: 'true', error: '', result };

Note: return in a Zapier JavaScript Code step exits the step's execution — the step succeeds and the current output value is used. This is useful for early-exit validation patterns.

Comparing Values: === vs ==

Always use strict equality (===) in Zapier Code steps. Zapier passes all inputData values as strings, so inputData.count == 5 may work (JavaScript coerces the string to a number) but inputData.count === 5 will always be false because the string '5' is not strictly equal to the number 5. Either compare string to string (inputData.count === '5') or convert first (parseInt(inputData.count, 10) === 5).

The conditional pattern that works best in Zapier Code steps: validate and assign early-exit output at the top, then write the main logic below with confidence that inputs are valid. Use lookup objects instead of long switch/if-else chains when the mapping is value-to-value. And always ensure output is assigned in every code path — the most common Code step failure is a condition that doesn't match and leaves output undefined.

For array filtering (a form of conditional operation on lists), see JavaScript array methods in Zapier Code steps. For round robin and randomization logic, see round robin and array randomization in Zapier. For help building conditional routing logic in a Zap, talk to Automation Ace.

ZapierJavaScriptCode Steps

Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.

Build Better Systems

Ready to automate with confidence?

Share your tools, process, and goals. Automation Ace can design the workflow, integration, AI assist, or code bridge that fits your business.

Start a Project