javascript or statement: Mastering the OR operator in JavaScript

Learn how the javascript or statement (||) works in JavaScript, with practical examples, patterns, and pitfalls for defaults, guards, and writing robust, readable code.

JavaScripting
JavaScripting Team
·5 min read
OR Operator in JS - JavaScripting
Quick AnswerDefinition

According to JavaScripting, the javascript or statement refers to the logical OR operator (||) in JavaScript. It evaluates operands from left to right and returns the first truthy value, or the last value if none are truthy. This behavior enables concise defaults using short-circuit evaluation, e.g., a || b returns a when a is truthy, otherwise b. Usage patterns include defaults, guards, and composing conditional expressions. Non-boolean values are coerced to booleans and the operator short-circuits, so later operands may not execute.

javascript or statement: Core concept

The javascript or statement refers to the logical OR operator (||) in JavaScript. It evaluates operands from left to right and returns the first truthy value, or the last value if none are truthy. This behavior enables concise defaults using short-circuit evaluation, e.g., a || b returns a when a is truthy, otherwise b. In practice, you often see it used to provide defaults, guard against undefined values, and compose conditional expressions. Remember that non-boolean values are coerced to booleans, and the operator short-circuits, so later operands won’t execute.

JavaScript
let a = 0; let b = \'hello\'; console.log(a || b); // hello
JavaScript
let a = \'first\'; let b = \'second\'; console.log(a || b); // first

Line-by-line breakdown:

  • Line 1–2 define operands
  • Line 3 applies || to pick the first truthy value
  • Line 4 logs the result

Common variations:

  • Use with variables carrying defaults
  • Combine with other operators for complex conditions
  • Pair with nullish checks when needed

formatType 3? notused: null

Steps

Estimated time: 20-40 minutes

  1. 1

    Create a small test file

    Set up a new JS file (e.g., or-demo.js) and import nothing. This keeps the scope focused on the || behavior so you can observe output clearly.

    Tip: Keep examples small and focused on one pattern at a time.
  2. 2

    Write OR-based defaults

    Implement a couple of defaulting patterns using ||, such as selecting a user name or a port number. Ensure you print results to verify behavior across truthy/falsy inputs.

    Tip: Use varied inputs to see how 0, '', and null affect the result.
  3. 3

    Run with Node.js

    Execute the file with node and inspect console output. Note how short-circuiting prevents evaluating the right-hand side when the left is truthy.

    Tip: Watch for side effects in the right-hand expression.
  4. 4

    Experiment with guards

    Create expressions that guard access to properties or nested values, demonstrating how || can simplify checks.

    Tip: Prefer short, readable guards over deeply nested ifs.
  5. 5

    Compare || with ??

    Add examples that compare || with the nullish coalescing operator (??) to clarify when each is appropriate.

    Tip: Highlight how ?? treats only null/undefined as missing.
  6. 6

    Refactor for clarity

    Refactor later examples into descriptive variables and functions, documenting intent with comments.

    Tip: Clear naming and minimal inline logic improve maintainability.
Pro Tip: Use || for simple defaults, but be mindful of falsy values like 0 and ''.
Warning: If you need to preserve valid falsy values, prefer ?? for nullish coalescing.
Note: Remember operator precedence with && and || when combining multiple expressions.

Prerequisites

Required

Optional

  • Familiarity with the browser console or Node.js REPL
    Optional
  • Optional: Knowledge of nullish coalescing (??) and optional chaining (?.)
    Optional

Keyboard Shortcuts

ActionShortcut
CopyIn editor or terminalCtrl+C
PasteIn editor or terminalCtrl+V
Format documentVS Code or compatible editor+Alt+F
FindSearch within editor or docsCtrl+F
Go to definitionCode navigationF12
Toggle integrated terminalEditor terminalsCtrl+`
Toggle line commentComment out linesCtrl+/

Questions & Answers

What is the javascript or statement in JavaScript and when should I use it?

The javascript or statement is the logical OR operator (||) in JavaScript. It returns the first truthy operand or the last operand if none are truthy, enabling defaults and guards in a concise form. Use it for simple fallbacks and short-circuiting conditional expressions.

The OR operator returns the first truthy value and stops; it's great for defaults and guards in JavaScript.

How does the || operator differ from the nullish coalescing operator ??

|| treats all falsy values as missing and picks the right-hand side, while ?? only treats null and undefined as missing. This distinction matters when 0, '', or false are meaningful values.

|| treats any falsy value as missing, while ?? only considers null or undefined missing, so 0 or '' can pass through.

Can I rely on short-circuiting in complex expressions containing && and ||?

Yes. && has higher precedence than ||, so expressions are grouped as a || (b && c). This affects results if any subexpression has side effects or if you rely on short-circuiting.

Remember, && binds tighter than ||, so complex expressions are grouped accordingly.

Is it safe to use || with functions or promises?

Using || with a function call will execute the function only if the left-hand expression is falsy. Be careful with side effects in the right-hand expression, especially with promises or async code.

Careful: the right side runs only if the left is falsy, so avoid side effects in the right-hand expression.

What are common pitfalls when using || for defaults?

Common pitfalls include unintentionally overriding valid falsy values like 0 or ''. For robust defaults, consider ?? or explicit checks.

Watch out for 0, empty string, and false—|| may replace them unless you intend it.

What to Remember

  • Master short-circuit evaluation with ||
  • Use ?? when you need nullish defaults
  • Beware of genuine falsy values in your data
  • Prefer explicit defaults for readability in complex expressions

Related Articles