Optional Multiplication Sign

It's common in math to omit the × symbol and write stuff like:

2x + 4(3x + 3log y + z) = 4sin(x) // mathematical jargon

Even some programming languages like Julia do this to make code look way cooler.

Examples

n * (n + 1)
n(n + 1) || (n + 1)n
2 * Math.PI
2Math.PI
(1 / 3) * 12 * getPerson().age // Convert years to months and divide by 3
(1 / 3)12getPerson().age // Kinda confusing, perhaps ok with proper highlighting?
(x - 4) * (3 * y + 2)
(x - 4)(3y + 2) // So better

You can also use this to rid the brackets in some cases.

2 / (3 * x)
2 / 3x

This not just improves readability but can also speed up finger scripting. Just a subtle yet nice addition. Your thoughts?

Note: The multiplier must immediately precede the multiplicand (with no whitespaces), otherwise, it's a syntax error. This is to rid some confusion.

These are already valid syntaxes for a function call.

nx
n12

These are valid identifiers.

You may be interested in GitHub - tc39/proposal-extended-numeric-literals: Extensible numeric literals for JavaScript, which uses the same syntax space and kind of offers the same semantics.

1 Like

As much as I would like to see a programming language resemble the notation of algebra, I can see this causing many problems; primarily logic errors.

1 Like

FWIW, Mathematica is one language I know that allows omitting the multiplication sign—two primary expressions (in ES terms) side-by-side is always a multiplication.

1 Like

Yeah, dumb of me not to notice that, but u know what? That's actually rather nice as it shuts the door to many confusing variations of the syntax. If we only allow for a number multiplier (on the left) and a multiplicand restricted to a numeric variable identifier, there would be zero ambiguity and it will be more than enough for most cases. This means we can still do stuff like 2Math.PI and 2 / 3x. Personally, I think it's best suited for those cases too. Bcuz, something like aDozenmangoes looks awful compared to aDozen * mangoes but 12 * mangoes is way awful than just 12mangoes. Get what I mean?

Just make sure you don't try to multiply with the "n" variable, otherwise you'll run into other pitfalls.

const n = 2;
const y = 3n;

What does y equal? The BigInt "3n". That's already valid syntax that can't break :).

1 Like