What is the JavaScript language?

JavaScript is a lightweight scripting language. The so-called "script language" means that it does not have the ability to develop an operating system, but is only used to write "scripts" that control other large applications (such as browsers).

JavaScript is also an embedded language. It does not provide a lot of core grammar itself, it can only be used to do some mathematical and logical operations. JavaScript itself does not provide any APIs related to I/O (input/output), and must be provided by the host environment (host), so JavaScript is only suitable for embedding in a larger application environment to call the underlying API provided by the host environment.

Currently, there are many host environments that have embedded JavaScript. The most common environment is a browser, and there is also a server environment, which is the Node project.

From a grammatical point of view, the JavaScript language is an "object model" language. Various host environments describe their own functions and operating interfaces through this model, thereby controlling these functions through JavaScript. However, JavaScript is not a pure "object-oriented language", it also supports other programming paradigms (such as functional programming). This leads to almost any problem, and JavaScript has multiple solutions. As you read this book, you will be surprised by the flexibility of JavaScript syntax.

The core syntax of JavaScript is quite concise, including only two parts: basic syntax constructs (such as operators, control structures, statements) and the standard library (that is, a series of objects with various functions such as Array, Date, Math etc.). In addition, various host environments provide additional APIs (that is, interfaces that can only be used in this environment) for JavaScript calls. Taking the browser as an example, the additional APIs it provides can be divided into three categories.

-Browser control: operating the browser -DOM class: manipulate various elements of the webpage -Web category: realize various functions of the Internet

If the host environment is a server, it will provide various operating system APIs, such as file operation APIs, network communication APIs, and so on. You can find these in the Node environment.

This book mainly introduces the core grammar of JavaScript and the basic knowledge of browser web development, without involving Node. The book can be divided into the following four parts.

-Basic grammar -Standard library -Browser API -DOM

There are multiple versions of the JavaScript language. The content of this book is mainly based on ECMAScript 5.1 version, which is the basis for learning JavaScript syntax. For the syntax of ES6 and newer, please refer to the ["Introduction to ECMAScript 6"] ( written by me.

Why learn JavaScript?

The JavaScript language has some notable features that make it very worth learning. It is not only suitable as an introductory language for learning programming, but also as a working language for daily development. It is currently one of the most promising and brightest computer languages.

The ability to manipulate the browser

The purpose of the invention of JavaScript is to serve as a browser's built-in scripting language to provide web developers with the ability to manipulate the browser. It is currently the only universal browser scripting language, supported by all browsers. It allows web pages to present various special effects and provide users with a good interactive experience.

Currently, almost all web pages in the world use JavaScript. If it is not used, the ease of use and efficiency of the website will be greatly reduced, and it will not become a convenient and user-friendly website.

For an Internet developer, JavaScript is an indispensable tool if you want to provide beautiful web pages, a satisfying Internet experience for users, various convenient browser-based functions, and close and efficient connections between the front and back ends.

Wide range of use

In recent years, the use of JavaScript has gradually surpassed browsers and is developing towards a universal system language.

(1) Platformization of the browser

With the advent of HTML5, the browser itself has become more and more powerful. It is no longer only able to browse the web, but more and more like a platform. Therefore, JavaScript can call many system functions, such as operating local files, operating pictures, and calling cameras. And microphones and so on. This allows JavaScript to accomplish many things that were unimaginable before.

(2) Node

The Node project allows JavaScript to be used to develop large-scale server-side projects, and it has become a reality that the front and back ends of the website are developed with JavaScript. Some embedded platforms (Raspberry Pi) can install Node, so JavaScript can develop applications for these platforms.

(3) Database operation

JavaScript can even be used to manipulate databases. The concept of NoSQL database itself was born on the basis of JSON (JavaScript Object Notation) format. Most NoSQL databases allow JavaScript to operate directly. The open source database PostgreSQL based on the SQL language supports JavaScript as the operating language, which can partially replace the SQL query language.

(4) Mobile platform development

JavaScript is also becoming the development language of mobile applications. Generally speaking, the Android platform uses Java language development, and the iOS platform uses Objective-C or Swift language development. Many people are working hard to make JavaScript the universal development language for various platforms.

The PhoneGap project is to package JavaScript and HTML5 in a container so that it can run on iOS and Android at the same time. Facebook's React Native project compiles components written in JavaScript into native components, so that they have excellent performance.

The mobile operating system Firefox OS of the Mozilla Foundation directly uses JavaScript as the platform language of the operating system, but it is a pity that this project did not succeed.

(5) Embedded script language

More and more applications use JavaScript as an embedded scripting language, such as Adobe's famous PDF reader Acrobat, and Linux desktop environment GNOME 3.

(6) Cross-platform desktop application

Operating systems such as Chromium OS and Windows 8 directly support JavaScript to write applications. Mozilla’s Open Web Apps project, Google’s Chrome App Project, GitHub’s Electron Project, and TideSDK project, can be used to write programs that run on multiple desktop platforms such as Windows, Mac OS and Android, without relying on browsers .

(7) Summary

It can be expected that JavaScript will eventually allow you to use only one language to develop programs that adapt to different platforms (including desktop, server, and mobile). As early as September 2013 in the Statistics, JavaScript was ranked first in terms of usage on GitHub that year Language.

The famous programmer Jeff Atwood even proposed a "Atwood's Law":

"Any application that can be written in JavaScript will eventually be written in JavaScript." (Any application that can be written in JavaScript will eventually be written in JavaScript.)

Easy to learn

Compared to learning other languages, learning JavaScript has some advantages.

(1) The learning environment is everywhere

As long as you have a browser, you can run JavaScript programs; as long as you have a text editor, you can write JavaScript programs. This means that almost all computers provide a JavaScript learning environment natively, without the need to install a complicated IDE (Integrated Development Environment) and compiler.

(2) Simplicity

Compared with other scripting languages ​​(such as Python or Ruby), JavaScript's syntax is relatively simple, and its own syntax features are not particularly large. Moreover, those complex parts of the grammar do not necessarily need to be learned. You can use simple commands to complete most of the operations.

(3) Similarity with mainstream language

The syntax of JavaScript is very similar to C/C++ and Java. If you have learned these languages ​​(in fact, most schools teach them), the introduction of JavaScript will be very easy.

It must be noted that although the core syntax is not difficult, the complexity of JavaScript is reflected in two other aspects.

First, it involves a large number of external APIs. For JavaScript to function, it must cooperate with other components. These external components are diverse and extremely large, and involve almost all aspects of network applications. It is no easy task to master them.

Second, the JavaScript language has some design flaws. Some places are quite unreasonable, and some places have weird results. Learning JavaScript, a large part of the time is spent figuring out where there are pitfalls. Douglas Crockford wrote a well-known book called "JavaScript: The Good Parts". The implication is that there are many bad things about this language. Write a book to make it clear. Other programmers feel that in order to write JavaScript programs more reasonably, they cannot be written in JavaScript, but must invent new languages, such as CoffeeScript, TypeScript, Dart, and the purpose of the invention of these new languages, more or less due to this factor.

Nevertheless, it seems that JavaScript's status cannot be shaken. In addition, the rapid evolution of language standards has made JavaScript functions increasingly enhanced, while grammatical defects and weirdness have been compensated for. Therefore, JavaScript is still worth learning, and it's really not difficult to get started.

Powerful performance

The performance advantages of JavaScript are reflected in the following aspects.

(1) Flexible grammar and strong expressiveness.

JavaScript not only supports clear procedural programming similar to C language, but also supports flexible functional programming, which can be used to write concurrent processing (concurrent). These grammatical features have proven to be very powerful and can be used in many situations, especially for asynchronous programming.

All values ​​of JavaScript are objects, which provides flexibility and convenience to programmers. Because you can create data structures easily and as needed at any time, without having to make troublesome pre-definitions.

The JavaScript standard is still evolving rapidly, and is constantly being rationalized, adding more applicable grammatical features.

(2) Support compiling and running.

Although the JavaScript language itself is an interpreted language, in modern browsers, JavaScript is run after being compiled. The program will be highly optimized, and the operating efficiency is close to that of a binary program. Moreover, the JavaScript engine is developing rapidly, and its performance will get better and better.

In addition, there is a WebAssembly format, which is the intermediate code format of the JavaScript engine, all of which are binary codes. Since the compilation step is skipped, the running speed close to that of the native binary code can be achieved. Various languages ​​(mainly C and C++) can be run in the browser by compiling into WebAssembly.

(3) Event-driven and non-blocking design.

JavaScript programs can adopt event-driven and non-blocking designs, which are suitable for high-concurrency environments on the server side, and ordinary hardware can withstand a large amount of visits.


JavaScript is an open language. Its standard ECMA-262 is an ISO international standard, which is very detailed and clear; the main implementations of the standard (such as V8 and SpiderMonkey engines) are open and of high quality. This ensures that the language does not belong to any company or individual, and there are no copyright and patent issues.

The language standards are formulated by the TC39 committee. The operation of the committee is transparent, all discussions are open, and meeting minutes are published.

The JavaScript runtime environments of different companies have good compatibility. The program can run on all browsers without adjustments or minor adjustments.

Community Support and Employment Opportunities

Programmers all over the world are using JavaScript. It has a huge community, extensive literature and books, and rich code resources. For most of the functions you need to use, there are multiple open source function libraries to choose from.

As the project leader, it is not difficult for you to recruit a large number of JavaScript programmers; as a developer, it is not difficult for you to find a JavaScript job.

Lab environment

This tutorial contains a lot of sample code, as long as the computer has a browser installed, you can use it for experimentation. Readers can run the examples while reading to deepen their understanding.

It is recommended to install the Chrome browser. The "console" in its "Developer Tools" is an ideal environment for running JavaScript code.

There are two ways to enter the "Control Panel" of the Chrome browser.

-Enter directly: Press Option + Command + J (Mac) or Ctrl + Shift + J (Windows / Linux) -Developer tool entry: The shortcut key of the developer tool is F12, or Option + Command + I (Mac) and Ctrl + Shift + I (Windows / Linux), and then select the Console panel

After entering the console, you can enter the code after the prompt, and then press the Enter key, the code will be executed. If you press Shift + Enter, the code will wrap and the execution will not be triggered. It is recommended that when reading this tutorial, copy the code to the console for experimentation.

As an attempt, you can copy the following program to the "console" and press Enter to see the running result.

function greetMe(yourName) {
  console.log("Hello " + yourName);

// Hello World