Skip to content

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

@samantasumit

Description

@samantasumit

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions