10

With the advent of "the web is the platform", JavaScript is fast becoming the most widely used programming language in the world.

However, many tasks that are easy to accomplish in other modern languages are difficult or puzzling in JavaScript, because the language design is so "unique":

  • JavaScript has a prototype-based OOP system; most other modern languages have an inheritance-based one
  • JavaScript has such a weird typing system that it doesn't fall into any of the standard categories (plenty of examples on WTFJS)
  • JavaScript is extremely lenient in how it parses the syntax (semicolons are optional etc.)

From a developer's point of view, most of these design decisions make developing in JavaScript harder. Conceptually, the JavaScript language seems one step behind Java, and two steps behind Python and Ruby. But since I don't believe that the people behind JavaScript are striving to create a language that is difficult to use, I can only reason that those design points must have something to do with the intended use of JavaScript as a scripting language for web sites, deeply connected to the DOM.

How (if at all) do these design points cater to the intended use cases of JavaScript? What reasoning is behind those seemingly random design decisions?

7
  • 6
    JavaScript was unleashed on the wider world on the back of the web browser, and in the same state of hurried half-bakedness. Nobody ever foresaw how important it would soon become as a de-facto standard for web apps, and therefore not enough care was taken to make it especially suited for that. That said, this issue is so subjective that I have no doubt the community will put it on hold. Feb 25, 2014 at 11:27
  • 2
    If they do, I would think it unfair. An answer like "JavaScript interpreters have to be lenient in their syntax checking to maximize compatibility with other JavaScript interpreters" would clearly fit the bill, and is everything but subjective.
    – user121228
    Feb 25, 2014 at 11:31
  • Also note that the DOM was something that was added into JavaScript much later, a lot of it was just it came out in a hurry and no one thought it was going to turn into this HUGE thing
    – Zachary K
    Feb 25, 2014 at 11:51
  • 1
    This question is partly answered in programmers.stackexchange.com/questions/221615/…
    – Euphoric
    Feb 25, 2014 at 11:57
  • 1
    "The language was absolutely not designed for programming in the large, and our implementation decisions, performance targets, and so on, were based on that assumption." -someone (worked on the original versions of JScript at Microsoft from 1996 through 2001.) from above referenced question
    – TruthOf42
    Feb 25, 2014 at 13:29

3 Answers 3

3
  1. Prototypical inheritance

    This appears to be because, despite what most claim, JavaScript is not a descendent of mostly Scheme, but Self. Self has prototypes and so does JavaScript.

  2. Implicit Coercions

    Neither Scheme nor Self had the invasive amount of implicit coercions that JavaScript has so we have to guess a little bit on why exactly they exist.

    It's quite reasonable to suggest that Eich was looking at the web and noticed that everything was/is, a string. It makes some level of sense then to say "Hey I know, let's save the programmer lots of tedious typing and just implicitly turn strings into whatever we want".

    Additionally, doing this rather than crashing means that while part of the page might render incorrectly because a string didn't hold a number, the page didn't crash. Which is arguably a much better UX. Occasionally doing something, even if wrong, is reassuring to the user.

    Remember that JavaScript was rushed to the market and was frozen prematurely, no one knew how much weird stuff was lurking in JavaScript's implicit conversions until it was too late. I won't debate in this answer whether they're good though :)

  3. Syntactic Sugar

    I suspect this has nothing to do with JavaScript being a web language and everything to do with JavaScript trying to be easy to use. Allowing optional semicolons and other syntactic sugar is incredibly common nowadays. I'd say it's more common to find a language made in the last 10 years that omits semicolons than requires them.

    This is all part of a trend towards JavaScript and other languages trying to optimize programmer time be making common patterns easy to type.

To wrap up, No, Kinda, No.

3

I think the question should be rewritten along the lines of "how did the strengths/weaknesses of internet/web browsers/etc. influence the design and implementation of Javascript?" -- in which case, I will delete my post -- because most of the question is irrelevant/wrong/cherry-picked/superficial examples that don't really indicate anything about Javascript's design.

By learning more about Javascript -- especially using it to implement some applications -- you will find out that your points are really just traps for the unwary, but become non-issues for more experienced Javascript developers. Understanding and taking advantage of Javascript's strengths, including this, lexical scoping, closures, higher-order functions, function literals turns out to be more important than getting tripped up by any of the weaknesses you mentioned. OTOH, an actual problem that must be addressed is the lack of a standard module system.

The only interesting sub-question is:

JavaScript has a prototype-based OOP system; most other modern languages have an inheritance-based one

I suspect this has something to do with memory limitations, but I can't find a reference for that. In addition, as @jozefg mentioned, there are other languages with prototype systems; it has different strengths/weaknesses than "mainstream" inheritance. Prototype-based object systems have their own rich history that you should learn about before dismissing them as mistakes.


However, many tasks that are easy to accomplish in other modern languages are difficult or puzzling in JavaScript

Pick any language, and pose the reverse question -- how many tasks that are easy to accomplish in Javascript are difficult or puzzling in other languages? What does the answer say about those other languages? (IMHO, not much -- it's always easy to cherry-pick examples, but much harder to show how those examples prevent people from getting work done)

JavaScript has such a weird typing system that it doesn't fall into any of the standard categories (plenty of examples on WTFJS)

What is weird about it? It's definitely not the only language that's dynamically-typed; neither is it the only one that allows coercions. Perhaps the number of coercions and some unexpected behavior in certain corner cases are distracting you.

JavaScript is extremely lenient in how it parses the syntax (semicolons are optional etc.)

How so? Do you have any other examples of leniency? FWIW, semicolons are optional sometimes; it's actually well-specified and therefore not what I would call "lenient".

If this is a problem for you, use a linter.

From a developer's point of view, most of these design decisions make developing in JavaScript harder.

How so? Can you provide examples? This is very subjective.

Conceptually, the JavaScript language seems one step behind Java, and two steps behind Python and Ruby.

How so? Can you provide examples? This is very subjective.

What reasoning is behind those seemingly random design decisions?

Why do they seem random?

2
  • 1
    I really liked the 2nd paragraph. I think it is a good rebuttal to people that say JS is terrible language, not sure if that was the point you were trying to portray.
    – pllee
    Feb 25, 2014 at 21:08
  • @pllee thank you! Yes, that's what I was going for.
    – user39685
    Mar 1, 2014 at 22:14
2

Doug Crockford has answered your question in the video "JavaScript: The Good Parts": http://www.youtube.com/watch?v=hQVTIJBZook

Just because JavaScript is different, it doesn't mean it's worse. The problems is when you try to make a classical inheritance in JS - but you shouldn't. Prototypes are quite great once you get to understand it.

BTW: Go (golang) has escaped classical inheritance in favor for embedding. Some people say it's liberating.

2
  • 2
    I think we should steer away from saying JS is good or bad, or imply as such
    – TruthOf42
    Feb 25, 2014 at 13:23
  • 1
    Good point & the point about Go is extremely relevant. But inheritance is do-able in javascript with late binding. It just doesn't have implicit inheritance.
    – sea-rob
    Feb 25, 2014 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.