Common Misconceptions about JavaScript

Written by

August 19, 2013


Things You Should Know about JavaScript    JavaScript logo

JavaScript was initially developed by Brendan Eich of Netscape in 1995 for their navigation browser, and in nearly two decades it has evolved further than Mr. Eich probably ever thought possible. Today JavaScript is the most popular client-side programming language not only in the world of web development, but also in other host environments that have been developed for use in different scopes. Despite its popularity, or perhaps due to it, there are a great number of misconceptions about JavaScript. Douglas Crockford, a senior JavaScript architect, claims that it is in fact the world’s most misunderstood programming languageË¡.

JavaScript has a particular set of features that makes it unique compared to other modern and wide-spread programming languages. It is not JavaScript’s concepts that are new; rather it is the combination of concepts packed into this language that make it unique. Whether you are an avid programmer, enjoy it as a hobby, or just want to sound smart at dinner parties, JavaScript is an incredibly valuable language to know and well worth understanding; so to help further your own knowledge, here are some common misconceptions and things you should know about JavaScript.

Java and JavaScript
One of the most common misconceptions about JavaScript (largely among those who don’t use it) is its relation to Java—namely that people think there is a relation between JavaScript and Java. There isn’t. While JavaScript is inspired by Java syntactically, they are totally different languages with different semantics.

Imperative vs. Functional JavaScript
Like most of the imperative programming languages, the syntax of JavaScript is originally derived from C. This point of origin gives the impression that JavaScript can be merely lumped in with other imperative language, but this over-simplified categorization obscures its value in functional programming—a widely neglected concept among JavaScript practitioners. This underutilization is unfortunate, because JavaScript has many constructs that are immensely helpful in functional programming: first class functions, higher-order functions, pure functions, anonymous functions and recursive functions. So don’t judge this book by its C-based cover, JavaScript is a great resource for functional programming.

Synchronous or Asynchronous JavaScript
JavaScript is primarily synchronous, although there are times that it functions asynchronously. JavaScript is a single-threaded language, which means it can only execute one piece of code at a time. A side-effect of being single-threaded then is that a long-running process can block the execution of other processes until it


is completed, potentially halting interaction and animation on the whole web page. This can be overcome to some degree synchronously by using an XMLHttpRequest (AJAX) as a supposition of long-running processes since it only pulls the information requested, rather than reloading the whole page.
Commonly though, JavaScript employs an asynchronous execution model for long-run processes in which timers, events and XMLHttpRequest are asynchronously managed under a single-threaded execution model, as illustrated in the image to the left.
Unfortunately asynchronous functions do not entirely solve this issue, and in fact timer callbacks under a single-threaded asynchronous model are notoriously inaccurate. For more information on how JavaScript timers work, I recommend John Resig’s blog².

Multi-Threaded JavaScript
While JavaScript is well-known as a single-threaded language, the advent of web worker has caused significant confusion on this point. Web Workers execute long-running script in the background without blocking the main JavaScript thread. This bypasses the limiting execution of long-running scripts (since asynchronous concurrency alone does not guarantee that there won’t be blocking) and allows the page to remain responsive. While workers allow for the concurrent execution of browser threads, it does not mean that it is multi-threaded JavaScript. Web workers are not a real feature of JavaScript itself, but rather a workaround by the host environment. Workers merely run the separate thread—they cannot access the document object model (DOM). Instead it interacts with the main JavaScript thread by passing messages.

Don’t get too excited for this option though, web workers aren’t a viable option for all instances of JavaScript; the start-up cost is fairly high and it can be resource-intensive, therefore it can really only be used for executing computationally expensive scripts.

Object-Oriented or Object-Based
JavaScript is an object-based programming language. Both object-based and object-oriented languages represent concepts as objects by encapsulating state and operations inside objects. There are key differences between the two however: object-based languages omit inheritance and sub-typing. JavaScript can simulate both of these features, but it does not carry any special constructs or sets of keywords to implement the concept of object orientation. Where object-oriented languages use classes for inheritance, JavaScript uses a prototype-based inheritance and performs behavior-reuse by cloning existing objects. A simple function construct is used to define a class, var makes the member private, and new helps to instantiate.Closure makes a persistent scope that manages the member items in its free variable pool and allows a function to access those, even when invoked from different lexical scope.

Lexical Scoping & Closure
Due to the functional programming style adopted in JavaScript, closure and lexical scoping concepts are specialized features and are a common source of confusion in JavaScript.

Lexical scope is the position of some entity when it is lexically analyzed in some programming constructs. Apart from the variables that are defined globally, all the variables which are declared using var are local to the respective scope. Lexical scopes can be hierarchical and every outer lexical scope would be considered containing free variables with respect to current scope. Lexical scopes always have upward visibility when they are hierarchically defined.
The closure is a specialized form of lexical scope, also known as the referencing environment; it is attached with a function that is exposed to other global or lexical scopes. Variables within this referencing environment are called the non-local or free variables of that function and are always chosen with respect to the lexical scope of the function. When this function is called, it gets all the variables maintained in its states or values. The concept of closure in JavaScript implements the behavior of encapsulation. Within constructor function local variables are considered private properties and public method

Data Types
Objects are dominant elements in JavaScript. Apart from few primitive data types, every concept is formed as an object; even the primitive data types have wrapper objects—e.g. var num1 = 10 and var num2 = new Number(10) represent the integer value, but both are of different types. Array, Date, Function and RegExp are also specialized forms of objects. All the data elements are first class citizens in JavaScript.

this keyword
this
is the most powerful keyword in JavaScript, but it is difficult to use without knowing its contextual nature. In most object-oriented languages this keyword is only available within the instantiate-able class construct and logically points to instance. A similar concept is available in JavaScript by implementing this in constructor-function called using a new keyword. In a global context and within normal functions this points to a global object (window).
If the function is name-spaced within some object then this points to the name-spaced object. The meaning of this keyword can be altered for a function on its call time; call and apply accept the first parameter as context that is internally treated as this.

Java Script host environment
While the web was JavaScript’s first platform, it is no longer limited to just this scope and in fact has become one of the most widely-reaching languages. The JavaScript interpreter and run-time environment are the typical actors to run the script. The run-time environment provides access to resources in the form of objects and methods, but JavaScript lacks specifications to define the way it loads libraries; instead each host environment manages it in its own way. JavaScript and its variants are embedded in a variety of tools and applications as a scripting language, and it was also added in many application development platforms such as Gjs, Node.js, Qt Quick, WinJs, and Phantom JS, which is a headless webkit based on JavaScript API.

It is this wide-spread popularity of JavaScript along with its valuable functionality that makes it such a valuable language to know and understand.

Notes

Ë¡ http://www.crockford.com/Java script/Java script.html

²http://ejohn.org/blog/how-javascript-timers-work/