GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Unexpected behavior with ternary operator syntax a ? : 1 in evaluate() · Issue #3578 · josdejong/mathjs

Unexpected behavior with ternary operator syntax a ? : 1 in evaluate() #3578

New issue
Copy link
New issue
Copy link
Closed
#3581
Closed
Unexpected behavior with ternary operator syntax a ? : 1 in evaluate()#3578
#3581
Copy link
Labels
bug

Description

@samantasumit
samantasumit
opened on Oct 31, 2025
Issue body actions

Description:

When using the evaluate() function in math.js, the expression 'a ? : 1' returns 1 even when a is truthy. This seems to contradict the expected behavior of the ternary operator, which should follow the format condition ? trueExpr : falseExpr.

Image

Steps to Reproduce:

const math = require('mathjs');
math.evaluate('a ? : 1', { a: 5 }); // returns 1

Expected Behavior:

This expression should either:

  • Throw a syntax error due to the missing trueExpr, or
  • Return undefined or null if trueExpr is omitted (depending on design choice), but not evaluate to the falseExpr when the condition is truthy.

Actual Behavior:

Returns 1, which is the falseExpr, even though a is 5 (truthy).

Environment:

math.js version: 14.5.2
Node.js version: 20.9.0

Additional Context:

This behavior may indicate a parsing issue or an undocumented fallback mechanism in the ternary operator implementation. It would be helpful to clarify whether this is intended behavior or a bug.

Activity

  1. gwhitney commented on Oct 31, 2025

    @gwhitney
    gwhitney
    on Oct 31, 2025
    Collaborator
    More actions

    There is indeed unexpected behavior, but the conditional is not evaluating the false expression when the condition is false, as the experiments math.evaluate('true?:2') (yielding 1) and math.evaluate('false?:2') (yielding 2) show.

    What is going on is that an empty expression, solely in the context of the true-expr slot of the conditional operator, is being interpreted as the constant 1.

    I would classify this as a bug (and marking the issue as such).

    The undesirable behavior is occurring because of an unfortunate interaction between the conditional operator ?: and the range operator : that both use the character :. What's happening is that when the parsing of the conditional operator requests the parse of the true-expr, the parser eventually comes to the point of considering whether that true part is a range. It eventually concludes that since this subexpression is in the position of possibly being part of a conditional, it must not interpret this occurrence of : as indicating a range -- but not before it has already applied the rule that a range expression with an empty first operand should default to a constant 1 as the first operand. It's at that point in the parse (the first check in the parseRange() function in src/expression/parse.js) that a true-expr of 1 is manufactured where none actually exists.

    The actual parse should be that the true-expr should be the same thing you would get as if you parse the empty string or a whitespace-only string, which, as it happens, turns out to be a constant value of undefined.

    I will file a PR shortly.

  2. gwhitney
    added
    bug
    on Oct 31, 2025
  3. josdejong commented on Oct 31, 2025

    @josdejong
    josdejong
    on Oct 31, 2025
    Owner
    More actions

    It makes sense to me that the parser would throw an error when either the truePart or falsePart of a conditional operator is missing. Would that be ok with you Glen?

  4. gwhitney commented on Nov 7, 2025

    @gwhitney
    gwhitney
    on Nov 7, 2025
    Collaborator
    More actions

    Ok, the fix has been revised so that an empty true expression is a syntax error. Hopefully ready for merging now.

    Reacted by Jos de Jong
  5. josdejong
    closed this as completedin #3581on Nov 12, 2025
  6. josdejong
    added a commit that references this issue on Nov 12, 2025

    fix: #3578 interpret empty true-expr of conditional as error (#3581)

    This issue will close once commit 7db7912 is merged into the 'develop' branch.
    7db7912
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.