8.1 What Is JavaScript and What Can It Do?

Larry Ullman, in his Modern Java Script: Develop and Design (Peachpit Press, 2012), has an especially succinct definition of JavaScript: it is an object-oriented, dynamically typed scripting language. In the context of this book, we can add that it is primarily a client-side scripting language as well. (We will discuss Node.js, a popular server-side implementation of JavaScript, later in this book in Chapter 13.)

JavaScript is object oriented, in that almost everything in the language is an object. For instance, variables are objects in that they have properties and methods (more about these terms in Section 8.7). Unlike more familiar object-oriented languages such as Java, C#, and C++, functions in JavaScript are also objects. As you will see in Chapter 10, the objects in JavaScript are prototype based rather than class based, which means that while JavaScript shares some syntactic features of Java or C#, it has significant differences from those languages.

JavaScript is dynamically typed (also called weakly typed) in that variables can be easily (or implicitly) converted from one data type to another. In a programming language such as Java, variables are statically typed, in that the data type of a variable is declared by the programmer (e.g., int abc) and enforced by the compiler. With JavaScript, the type of data a variable can hold is assigned at runtime and can change during runtime as well, as can be seen in the following example.


let count = 23;
count = "hello";
count = true;

The final term in the aforementioned definition of JavaScript is that it is a client-side scripting language, and due to the importance of this aspect, it will be covered in a bit more detail in the following sections.

8.1.1 Client-Side Scripting

The idea of client-side scripting is an important one in web development. It refers to the client machine (i.e., the browser) running code locally rather than relying on the server to execute code and return the result. There are many client-side languages that have come into use over the past two decades including Flash, VBScript, Java, and JavaScript. Some of these technologies only work in certain browsers, while others require plugins to function. We will focus on JavaScript due to its browser interoperability (that is, its ability to work/operate on most browsers). Figure 8.1 illustrates how a client machine downloads and executes JavaScript code.

Figure 8.1 Downloading and executing a client-side JavaScript script

The image contains 6 steps and 2 browser windows, 1 server window and 1 code block that shows downloading and executing a client-side JavaScript script.

There are many advantages of client-side scripting:

The disadvantages of client-side scripting are mostly related to how programmers use JavaScript in their applications. Some of these include the following:

Despite these limitations, the ability to enhance the visual appearance of a web application while potentially reducing the demands on the server make client-side scripting something that is a required competency for the web developer. Understanding the fundamentals of the language will help you avoid JavaScript’s pitfalls and allow you to create compelling web applications.

Note

It should be stressed that JavaScript and Java are vastly different programming languages with very different uses. Java is a compiled, object-oriented language, popular for its ability to run on any platform with a Java Virtual Machine installed. JavaScript is one of the world’s most popular languages, with fewer of the object-oriented features of Java, and usually runs directly inside the browser, without the need for the JVM. Although there are some syntactic similarities, the two languages are not interchangeable and should not be confused with one another. As wonderfully summed up by Kyle Simpson in his You Don’t Know JavaScript series (O’Reilly, 2015), “JavaScript is as related to ‘Java’ as ‘Carnival’ is to ‘Car.’”

8.1.2 JavaScript’s History

JavaScript was introduced by Netscape in their Navigator browser back in 1996. It originally was called LiveScript but was renamed partly because one of its original purposes was to provide a measure of control within the browser over Java applets.

Internet Explorer (IE) at first did not support JavaScript but instead had its own browser-based scripting language (VBScript). While IE soon supported JavaScript, Microsoft sometimes referred to it as JScript, primarily for trademark reasons (Oracle currently owns the trademark for JavaScript).

To muddy the waters further, Netscape submitted JavaScript to Ecma International, a private, nonprofit standards organization. Formerly approved in 1997, ECMAScript is simultaneously a superset and subset of the JavaScript programming language. That is, the JavaScript that is supported by your browser contains language features not included in the current ECMAScript specification while also missing certain language features from that specification.

At the time of writing (spring 2020), the latest version of ECMAScript is the Tenth Edition (generally referred to as ES11 or ES2020). The Sixth Edition (or ES6) was the one that introduced many notable new additions to the language (such as classes, iterators, arrow functions, and promises). Editions 7 through 11 added only a relatively modest number of additions in comparison. There is also an ES.Next, which is the on-going name for proposals of new features to the language.

8.1.3 JavaScript and Web 2.0

One of this book’s authors first started teaching web development in 1998. At that time, JavaScript was only slightly useful, and quite often, very annoying to many users. Back then JavaScript had only a few common uses: graphic rollovers (that is, swapping one image for another when the user hovered the mouse over an image), pop-up alert messages, scrolling text in the status bar, opening new browser windows, and prevalidating user data in online forms.

It wasn’t until the middle of the 2000s with the emergence of so-called Web 2.0 or AJAX-enabled sites that JavaScript became a much more important part of web development. AJAX is both an acronym as well as a general term. As an acronym it means Asynchronous JavaScript and XML, which was accurate for some time. Over the past decade, JSON (JavaScript Object Notation) has almost completely displaced XML as the most common format for data transport in interactive web sites, thus making the AJAX acronym less and less meaningful today. Nonetheless, this book occasionally makes use of the AJAX term simply to refer to interactive web pages that asynchronously consume data from external web APIs.

Note

It is important to remember that while new features can be added to the language specification, ultimately it is the browsers themselves that determine the extent to which new features will be supported. There is sometimes a considerable lag between when a new language feature is defined within the ECMAScript specification and its universal support in all browsers. Online tools such as caniuse.com can help you determine browser support for newer additions to JavaScript and CSS.

The AJAX-style of web interactivity was initially enabled by the XMLHttpRequest object introduced by Microsoft in their Internet Explorer browser way back in 1999. By the mid 2000s, sites such as Google, Gmail, and Maps demonstrated the interactive power of these AJAX techniques. Thanks to the spread of AJAX techniques over the subsequent decade, the nature of contemporary web development is today very JavaScript centric. As can be seen in Figure 8.2, back-end coding has become much “thinner” as more and more application logic has moved into the front end.

Figure 8.2 Contemporary JavaScript coding

The image contains 9 steps, 2 browser windows and 2 server windows. The image shows Contemporary JavaScript coding.

8.1.4 JavaScript in Contemporary Software Development

While JavaScript is still predominately used to create user interfaces in browser-based applications, its role has expanded beyond the constraints of the browser, as seen in Figure 8.3.

Figure 8.3 Contemporary JavaScript coding

The figure has 12 images that show the usage of JavaScript for creating user interfaces in browser based applications.

Thanks in part to Google, Mozilla, and Microsoft releasing V8, SpiderMonkey, and Chakra (their respective JavaScript engines) as open-source projects that can be embedded into any C++ application, JavaScript has migrated into other non-browser applications. It can be used as the language within server-side runtime environments such as Node.js. Some newer non-relational database systems such as MongoDB use JavaScript as their query language. Complex desktop applications such as Adobe Creative Suite and OpenOffice use JavaScript as their end-user scripting language. A wide variety of hardware devices such as the Oculus Rift headset and the Arduino and Raspberry Pi microcontrollers make use of an embedded JavaScript engine. Indeed, JavaScript appears poised to be the main language for the emerging Internet of Things.

Part of the reason for JavaScript’s emergence as one of, or perhaps even, the most important programming languages in software development is the vast programming ecosystem that has developed around JavaScript in the past decade. This ecosystem of JavaScript libraries has made many previously tricky and tiresome JavaScript tasks much easier.

These libraries of JavaScript functions and objects are generally referred to as JavaScript frameworks. Some of these frameworks extend the JavaScript language; others provide functions and objects to simplify the creation of complex user interfaces. The past several years have witnessed a veritable deluge of new JavaScript frameworks. User interface frameworks such as React and Vue.js simplify the process of creating Single-Page Applications (SPA), while more complex MVC frameworks such as Angular and Ember allow developers to construct applications using software engineering best practices. JavaScript can even be used to author native desktop or mobile applications using frameworks such as React Native or Electron. You will learn more about React, currently the most popular JavaScript framework, in Chapter 11.