Is JavaScript the best language for new coders?

Is JavaScript the best language for new coders?Matthew MacDonaldBlockedUnblockFollowFollowingMar 19Despite its many flaws, JavaScript just might be the best tinkering language for kids and new learners.

The year was 2007.

O’Reilly Media, a top-shelf technical publisher, had approached me with an idea.

Would I like to write a book introducing JavaScript to complete beginners?My response was immediate: Why?It wasn’t that JavaScript was useless.

But JavaScript was just one ingredient in a careful arrangement of technologies needed to build a website — and it was by far the least important one.

Yes, a sprinkle of JavaScript pixie dust could validate long forms or make a button glow when someone hovered over it.

But JavaScript was optional — take it out of a page, and everything kept working in more or less the same way.

JavaScript also came with some serious baggage.

It was tied up with browser compatibility issues, slow, insecure, and distinctly unreliable.

I had no interest in teaching someone how to create a useless JavaScript-powered web page widget.

That idea seemed as pointless as the animated gifs of rotating globes that were all the rage in the early days of the Internet.

The most obvious way to use JavaScript, or so I naively thought at the time, was to let your big server-side¹ framework render whatever JavaScript it needed directly into the page.

Yes, that JavaScript might be a bit bloated, but it was tailored to suit the browser on the other end, and nothing ever broke — even if the visitor turned off JavaScript completely.

Needless to say, I didn’t write the book.

I kept coding on the web server and writing about my favorite technologies (usually C# and the .

NET Framework).

Years passed.

Then something strange happened.

JavaScript took over.

I still don’t think JavaScript deserves to be called the best choice in programming languages.

I don’t even think it’s a particularly good language, if we’re being completely honest.

But I have changed my mind about one thing.

Now I believe that JavaScript just might be the best starting point for kids and new coders.

JavaScript’s incredible reachThe one unrivaled advantage that JavaScript has is its reach.

JavaScript is everywhere — supported on every operating system, in every brand of browser, and on desktop computers and mobile devices alike.

Just as important is the fact that JavaScript doesn’t require any deployment.

In fact, it’s already hard to remember the not-so-distant past when companies would roll out client-server applications² on their internal networks, spending weeks wrestling with setup issues and unexpected errors.

Faced with those nightmares, you can see JavaScript’s appeal.

Thanks to its reach, JavaScript didn’t need to become the best programming language.

It only needed to become good enough.

A similar dynamic is at work when it comes to learning to code.

At first, the issue of reach doesn’t seem as important for a person who’s only goal is to learn good programming practices.

But many of the same issues come into play with learning to program as in professional development.

If you want your work to have universal reach, JavaScript is the only candidate.

For example, say you’ve built an amazing personality-predictor-quiz app.

Setting up a development environment is relatively easy.

But what happens when you want to send your program to a friend?.Do they need to install a runtime or set up the same development environment?.Does their computer need its security settings tweaked before it allows them to download and install your program?.Maybe your friend is using an operating system that doesn’t support your program at all — or a mobile platform that only allows professional applications that are distributed in iTunes or the Google Play store.

With JavaScript and a basic website (GitHub will give you a small chunk of space for free), these issues disappear.

And if the new coder-to-be is a kid, here’s an indisputable fact: kids and browsers are a tight fit.

If you’ve watched a child at work on a computer (not a mobile device), you’ve probably noticed that at least 98% of their time is spent in a browser.

They play games there, use social media, and review schoolwork through Google classroom and Google Docs.

It’s only natural that the code they create is consumed inside the same browser world that they already inhabit.

Now for the problems…But what about the problems?.JavaScript has some serious drawbacks, and its quirks, inconsistencies, and limitations can be particularly painful for someone who’s learning to program for the first time.

Fortunately, there are modern solutions that can prevent most of the pain.

Let’s look at the top four most common complaints.

1.

JavaScript isn’t type safeOne key concept new programmers learn early on is the idea of variables, containers that store information over the life of a program.

The problem with JavaScript is that it’s loose and sloppy with variables.

It accepts things that don’t seem right and ignores obvious problems.

Its laziness can turn small typos into code-crashing disasters.

Here’s an example of some code that’s doomed to fail:var myNumber = 100;myNumber = myNumbr + 1;Did you catch the mistake?.The second line actually uses two variables: the myNumber variable that was defined in the first line and an imaginary myNumbr variable.

We know that second variable is the result of a typo, but JavaScript helpfully “fixes” the problem by creating a new myNumbr variable on the fly, which causes the calculation to fail silently.

The same issue happens if you use a variable name with inconsistent capitalization (like MyNumber instead of myNumber).

A similar problem occurs if you think a variable is holding a number when it really has a piece of text, and so on.

Every JavaScript developer has a JavaScript horror story like this.

It’s hard enough to learn to code without needing to worry about the ways your programming language is sabotaging you at the same time.

But, happily, type safety problems are easily fixed with the right development tool.

One of my favorite code editors — both for learning and for professional use — is Visual Studio Code.

It’s free, lightweight, open source, and endlessly extensible.

One of its best features is its ability to error-check JavaScript code for common problems.

You can turn on error-checking by adding a configuration file, or by sticking this comment at the top of a JavaScript file:// @ts-checkHere’s an example that has two mistakes, neither one of which is a problem in JavaScript’s eyes:Credit: Matthew MacDonald ©If you use the ts-check comment, Visual Studio helpfully underlines these potential mistakes:Hover over one of these underlined areas with the mouse, and Visual Studio pops up an explanation of the problem:The takeaway?.Although JavaScript tolerates plenty of bad programming practices, if you combine JavaScript with a good code editor you can get an experience that’s similar to other modern programming languages.

And you haven’t given up JavaScript’s reach.

2.

JavaScript doesn’t do OOPObject-oriented programming (OOP) is a way to model and arrange code.

Done right, OOP helps programmers create simpler, better organized code.

It also makes it easier to reuse important bits of functionality.

JavaScript is notoriously lacking in support for object-oriented programming.

In fact, JavaScript developers routinely fake it with awkward techniques and odd workarounds.

These techniques might make sense to someone who’s already learned to program (and even if they don’t, you can simply copy the pattern into your own projects and learn to live with it).

But if you’re completely new to coding, using a kludge for a basic programming concept is an obvious fail.

Fortunately, there are better, more elegant workarounds that are properly integrated with the JavaScript language.

My favorite is TypeScript, an open source project launched by Microsoft in 2012.

TypeScript works as a sort of cleaned-up version of JavaScript that adds object-oriented programming (along with other helpful improvements, like strict type checking).

Some might object that TypeScript isn’t exactly JavaScript — and that’s true.

But here’s the magic part.

You can write your code in TypeScript, and then convert it to JavaScript before you use it in a web page.

This gets you the best of both worlds: a modern programming language for writing your code, and the same universal support for running your code as ordinary JavaScript.

Best of all, this whole process happens automatically if you use a tool like the Visual Studio Code.

Yes, the generated JavaScript code will still use odd workarounds.

But so what?.The programmer gets to learn OOP concepts, the finished code works without a hiccup, and modern computers handle the whole mess with ease.

Don’t believe me?.Some of today’s most innovative desktop applications use a combination of TypeScript and Electron, a framework that allows JavaScript to run outside of a browser.

One of the most impressive examples is Visual Studio Code.

That’s right — the same slick tool you use to write JavaScript code was also written with JavaScript code.

More specifically, it was written using the modern TypeScript flavor of the JavaScript language.

Of course, you don’t need to use TypeScript, and new JavaScript programmers can get a lot done without learning any OOP principles, if they so choose.

It’s really a matter of choice.

Either way, hope is on the horizon: JavaScript is still being enhanced, and newer versions are slowly adding some of the improvements from TypeScript.

3.

The world is full of bad JavaScript codeHave you ever heard of Visual Basic?4.

JavaScript needs additional libraries and frameworksTo get access to more functionality (and to avoid reinventing the wheel), JavaScript coders need to use other components, libraries, and frameworks.

Choosing the right ingredients — ones that work well, will be supported long into the future, and won’t conflict with one another — isn’t as easy as it seems.

Most languages face a version of this problem.

However, few languages suffer from this situation as badly as JavaScript.

To become a serious programmer in the free-for-all JavaScript world, you need to assemble your own stack of development tools and add-ons, choosing from options that are so complex and convoluted you won’t know if one suits you until you’ve thoroughly learned it (at which point it may have drifted into obsolescence, replaced by the latest hot new fad).

As bad as these headaches are, they don’t really affect new coders.

If someone is learning to code with JavaScript, the best approach is to stay away from frameworks and libraries, whether we’re talking jQuery, Angular, React, Vue, or something that’s been invented between the time I typed this sentence and the moment I hit publish.

Eventually, after a new coder has a solid command of programming fundamentals, they’ll probably want to explore at least one of these frameworks.

But that’s a topic for another day.

Matthew MacDonald is a coder and former developer evangelist.

He’s currently writing a coding book for kids using JavaScript.

¹ Server-side code is the code that runs on a web server, far away from your computer.

Client-side code is the code that runs right in your browser.

For example, search for a product on Amazon and server-side code scours Amazon’s databases, generates the HTML with a list of possible matches, and sends it to your browser.

² Client-server applications are an older model of application that divides the work into two parts.

One part runs on a desktop computer (the client), which communicates with the other part (the server) on another computer.

.. More details

Leave a Reply