TOC

This article is currently in the process of being translated into Vietnamese (~42% done).

About JavaScript:

JavaScript and the webbrowser

Với nhiều người, Javascript và trình duyệt web có quan hệ rất gần gũi. Điều này có vẻ hợp lý, do Javascript ban đầu được tạo ra bởi chính đội ngũ của trình duyệt web Netscape Navigator và chỉ được dùng duy nhất trên nền tảng web. Đó là lý do vì sao Javascript thường được gọi là "ngôn ngữ lập trình an toàn", vì trong dạng triển khai phổ biến nhất của nó, nó không bao giờ cho phép quyền truy cập vào hệ thống tập tin hoặc những thứ khác tương tự - thay vào đó, nó giống như một hộp cát nơi bạn có thể làm những thứ "tốt", không liên quan đến hệ thống.

Tuy vậy, nhất thiết nên nhớ rằng Javascript có thể được sử dụng bên ngoài trình duyệt web, Javascript sử dụng một bộ máy Javascript, hay trình thông dịch, để thực thi. Trình thông dịch này có thể được chạy trong một trình duyệt, và gần như trong tất cả trình duyệt web được biết đến, nhưng không có gì ngăn cản trình này chạy ngoài trình duyệt web. Trên thực tế, điều này đã được thực hiện rất nhiều lần.

Node.js

Một ví dụ cho điều này là dự án Node.js. Được sáng lập vào năm 2009, nó cơ bản là đưa trình thông dịch Javascript ra khỏi trình duyệt web và thêm các mô-đun để khiến Javascript trở thành một ngôn ngữ lập trình đa dụng thay vì là một ngôn ngữ kịch bản dùng trong trình duyệt. Điều này kéo theo lượng quan tâm mới và ở mức độ cao hơn rất nhiều cho Javascript, bởi vì giờ đây bạn có thể sử dụng các kĩ năng từ Javascript cho cả web và tạo những mã kịch bản cho máy chủ. Và do Node.js là đa nền tảng, nó đột ngột dẫn đến cho phép bạn thực thi các mã nguồn của bạn trên Windows, Linux, Unix, và MacOS, mà không cần một trình duyệt web.

The host environment

All of this leads us to the discussion of the host environment. The host environment is where your JavaScript is executed, and usually it includes functionality not directly found in the JavaScript language. The most obvious example of this is of course the webbrowser, which adds a LOT of functionality, allowing your JavaScript code to communicate with the browser and manipulate the contents of the underlying website.

Another hosting environment could be Node.js, adding serverside related functionality, but since JavaScript was originally designed to be able to run anywhere, this could also be your coffee machine. It could easily include a JavaScript engine and add functionality related to making coffee and taking instructions from push buttons instead of a website.

With or without a browser?

So, as you can see, JavaScript can definitely run outside the webbrowser. That's true now, and I can only imagine that the increasing popularity of JavaScript will make this even more true in the future. Therefore, it's important for me to stress that in this tutorial, we will first and foremost learn the JavaScript language.

So, in the first part of this tutorial, we will focus on the actual language and not talk too much about how it can interact with a browser. With that in mind, it would be silly to completely ignore how JavaScript interacts with the webbrowser since this is still such a common use case. Therefore, the latter part of this tutorial will be dedicated to exactly this subject, where we will dig deeper into the ties between JavaScript and the webbrowser.

A little heads-up to the purists out there: You might notice that even though we keep the browser out of the equation during the first part of the tutorial, I will be using a couple of functions which could be considered browser related. An example of this is the alert() function, which will show a simple message box in the browser. I use it because it's simple and easy to understand in the examples we use in this tutorial, and because it produces a result which can be seen when reading the tutorial in a webbrowser and running the examples directly from the website.

Summary

JavaScript is often run inside a webbrowser, but not always. In this tutorial we will focus on the core JavaScript language first, allowing you to learn the language, and then later on, we will look into how JavaScript can interact with a webbrowser. However, we will be showing you examples where we interact a bit with the browser, simply because it's easy to understand and can be tested while you read the articles.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!