Javascript – Why don’t languages use the words “and” and “or” instead of “&&” and “||”

javascriptoperatorssyntax

When I was a beginner it took a while to learn the language syntax and the idea that languages couldn't improve after they were invented.

But now we're seeing new language features added every year with ES5, ES6 the so on.

If I don't write software for a few weeks or months when I come back to it I have often found for the first few days found myself writing if statements with the literal words and and or.

if (isSinglePost and isLoggedIn) {
   // do something
}

AND I've seen other languages use literal and and literal or in their language syntax.

I'd like to have the option to use and and or or || and &&. I'm not saying it's the best practice but it would be nice to have that option for readability and writability.

So now I'm wondering is there any reason languages didn't use and and or in their language syntax in the first place when they were invented? And would there be any negatives to adding it now?

ES2019candidate

Best Answer

In short

It's historical reasons.

The long history

Many older languages created between the 50's and the end of the 60's, as well as used the logical operators that you like such as not or and and:

  • Fortran II, 1961, introduced logical operators between dots with .NOT. .AND. .OR.
  • BASIC, 1964, (although I'm not sure that it had these operators in the very first version)
  • Simula 67, used them as keywords for the more concise and mathematically inspired ¬ for not, (and=intersection) and (or=union).
  • Algol68,1968, used them as a portable alternative to ¬, and
  • Pascal, 1970,

Their modern descendants (e.g. ADA) have kept this keyword style.

You can however see that already in this first list, there was a quest for concise expressions in many languages. But the character sets in those years were not portable and the later work on the ASCII character set didn't let many of the special characters survive.

Other languages used also the concise approach but chose characters that were more lucky in the standardisation process (for example see here the rationale that lead to the inclusion of | in the ASCII character set):

  • PL/I, 1964, used & for and, | for or and ¬ for not
  • BCPL, 1967 used & for and, | for or and ~ for not. It also offered keyword alternatives. But those were not so appealing: LOGOR, LOGAND and LOGNOT
  • Finally came C, 1972, that had the incredible growth that we know. C was inspired (indirectly via B) from BCPL. It is not surpriseing that its authors, Kernighan & Ritchie, took over the & and |. But as C is system oriented these were taken as bitwise operators. K&R identified also the need to have short circuit operators for conditional expressions to know that they can skip the rest of the expression if it's already known that it's true or false (the purpose was to write concise error checking conditions). And for these logical operators, they just doubled the symbol, so && and ||

Then came C++ inspired by C, then Java inspired by C++ then JavaScript inspired from Java... and this is why nowadays so many languages have opted for the well known || and &&

P.S.: Note, that if JavaScript would have adopted and or rather than && || , it would probably have adopted begin .. end rather than { .. } , making it overall a lot more verbose than we are used to ;-)

P.S.2: Note, that psshill points out in comment that C++ funilly supports and, or, bitand, bitor and a couple of other alternative tokens. But nobody uses them. Interestingly, these are not a recent language features: Stroustrup explains in his book "The design and evolution of C++" that these keywords were introduced by the C++ ISO committee in November 1993, because in that pre-unicode world, ISO-646 used the ascii code of []{} and | to map European characters, which made C++ very complex on terminals using this encoding. Strangely, though, there is no begin ... end to replace {...} and instead <% and %>. I guess that the alternate keywords not really won traction, because around the same period ISO-8859 encoding started to be used with all ascii characters available. Usage and habits did certainly do the rest: Stroustrup reports highly controversial discussions around alternative tokens.