The language series: JavaScript


I was pretty much amazed to see a JavaScript library for Arduino last year; it’s common knowledge that the language powers uncountable web sites, mobile applications and even Windows 8 apps, but Arduino? Mind-blowing. The ever-growing need for powerful web experiences propels the adoption and development of this remarkable language. 

JavaScript was influenced by C (syntax), Java (naming conventions), Scheme (functional programming and lambdas) and Self (Prototypical inheritance). The rushed development and deployment of the language explains some of the flaws in the language;  however, it exposes a powerful and beautiful core once you know it well enough to fully leverage the benefits of prototypical inheritance (good for programmers to know), functional programming and its unique development idioms.

My JavaScript Story

I started learning JavaScript in 2011 after copy-pasting a gazillion snippets off the internet (yes, I was guilty of that).  My first read was jqfundamentals and then eloquent JavaScript; both are excellent books by the way. However, I learnt nearly all the JavaScript I now build on during a 12-week internship; it was an awesome but sometimes grueling experience – I had to read Douglas Crockford’s book, dig into jQuery and follow first-rate dev methods and practices.

The bad parts

  • What is this? (Pun intended). It can refer to the window object or the object instance it was called on. Be careful with this. :D
  • Addition and subtraction can be string concatenation or mathematical operations. See below for examples.
  • The == and != operators carry out implicit type coercion, you have to use the === and !== operators which check for type and value instead.
  • The evil eval has led to security issues, avoid it totally. A few good uses exist though but you should still avoid it.
  • The parser automatically inserts semi-colons after statements; leads to weird bugs. Make sure you add semi-colons where they should be.
  • Global variables, you forgot to declare some variable properly? No problem, JavaScript stuffs it in the global object. Good luck finding these.
  • Inconsistencies in browser support.

The Good parts

  • First-class functions and lambdas; whoot!
  • Prototypical inheritance; a powerful object model as objects are not limited to being instances of a single class.
  • Loose typing boosts expressiveness and ease of use.
  • Cross-platform and widely-used; in fact you’re using JavaScript now.
  • The good subset of the language is really beautiful and powerful.
  • Closures are pretty cool.
  • Lots of support, there seems to be a never-ending stream of new fancy libraries, frameworks and utilities.

Weird JavaScript

Now some of these are truly incredible; if you don’t believe me, fire up the console in your browser and type them in!

  • ‘5’ + 3 = ’53’ while ‘5’ – 3 = 2.
  • “” == 0 and 0 == “0” evaluate to true but “0” == “” is false, shouldn’t equality be transitive?
  • 0.1 + 0.2 !== 0.3; in my browser, the result is 0.30000000000000004; however I really don’t trust floating point operations anyway as computers have issues with them. Best thing is to always to use some epsilon value as a buffer range.
  • typeof([]) === typeof({}) === typeof(null) === “object”.
  • [] + {} = “[object Object]” but {} + [] = 0!
  • NaN !== NaN

How Most People learn JavaScript

A lot of people get into JavaScript by copying useful code chunks on the internet and plunking them into their codebases (I still haven’t met anyone who explicitly chose to learn JavaScript). Afterwards, they need more control and expressive power so they decide to integrate and leverage a library (most probably jQuery). Ultimately, most people find themselves learning more about their preferred library.

Next, they go ahead to learn the language itself by reading a couple of books about it and attending conferences. The JS enlightenment comes handy in understanding the quirky behaviour of the language and makes it easy to use huge big frameworks and libraries (e.g EmberJS, backbone, etc). In the long run, motivated programmers eventually start writing their own plugins and frameworks.

Tip

Great tools like JSHint and JSLint help spot potential pitfalls and save you from tearing out all your hair in frustration, agony and annoyance. They also reduce the chance that there’ll be some irate developer in the future looking for ways to hurt you or cursing you every single day.

Rating

8.1/10

An easy language to use – most programmers actually started out by copying code snippets without fully understanding the language and slowly grew to learn it. It runs on the client side, server-side and there are wrappers for most devices. JavaScript is a dynamic prototypical loosely typed functional programming language with some ‘swagger‘ at its core.

Did you enjoy this post? Check out my earlier posts on C, PythonJava and PHP.

6 Comments

·

Leave a Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s