Why doesn't JavaScript have explicit words for logical operations?

Reasons for this question

  1. Python is a programming language that has a very interesting philosophy... Called The Zen of Python. On this, does Javascript have a philosophy just like Python... as The Zen of Javascript?
  2. In Python philosophy there is something like the law "Explicit is better than implicit." Why doesn't JavaScript have explicit words for logical operations? What is the philosophy behind javascript?
  3. My goal in this question is to know a curious fact ... my goal is not to criticize JavaScript ... but know which philosophy behind JavaScript ...
  4. I didn't eat information about it

Some examples...

case 1: or

case1.1: example1.js

alert( true || true );   // true
alert( false || true );  // true
alert( true || false );  // true
alert( false || false ); // false

case1.2: example2.js

alert( true or true );   // true
alert( false or true );  // true
alert( true or false );  // true
alert( false or false ); // false

case2.1: example1.js

result = a || b;

case2.2: example2.js

result = a or b;

case3.1: example1.js

if (1 || 0) { // works just like if( true || false )
  alert( 'truthy!' );
}

case3.2: example2.js

if (1 or 0) { // works just like if( true || false )
  alert( 'truthy!' );
}

case 2: and

case1.1: example1.js

alert( true && true );   // true
alert( false && true );  // false
alert( true && false );  // false
alert( false && false ); // false

case1.2: example2.js

alert( true and true );   // true
alert( false and true );  // false
alert( true and false );  // false
alert( false and false ); // false

case 3: not

case1.1: example1.js

alert( !true ); // false
alert( !0 ); // true

case1.2: example2.js

alert( not true ); // false
alert( not 0 ); // true

references

Because JavaScript's author decided to model its syntax after Java, which was the popular language of the time. (at least, that's how I remember the history, someone can correct me if I'm wrong or misinformed). At this time, most languages were using symbols instead of keywords for operators, so it makes sense that JavaScript followed this legacy, they didn't have a strong reason to deviate. Plus, it gives JavaScript a familiar feel for those coming from other languages. And, considering it was built in two weeks, it's probably better to follow the status quo, because you don't have time to try and invent new ideas.

So, both JavaScript and Java use || for "or", and && for "and". They both use ! for "not". They both have similar switch statements, and the same case-fallthrough pitfalls. The syntax looks identical for for-loops, ifs, whiles, etc. Of course, the actual language is very different from Java, but its syntax is pretty close.

Note that some of JavaScript's newer syntax deviates from these patterns a bit. "await", "yield", etc, are all keywords, they're not operators. But, we still use operator-based syntax as well, like with "??", but perhaps that's for the better, what keyword would you give to "??"? I can't think of any concise one.

5 Likes