The Javascript Language: Ilya Kantor
The Javascript Language: Ilya Kantor
The Javascript Language: Ilya Kantor
The JavaScript
language
Ilya Kantor
Built at August 16, 2020
An introduction
About the JavaScript language and the environment to develop with it.
An Introduction to JavaScript
Let’s see what’s so special about JavaScript, what we can achieve with it, and which other
technologies play well with it.
What is JavaScript?
Scripts are provided and executed as plain text. They don’t need special preparation or
compilation to run.
In this aspect, JavaScript is very different from another language called Java .
But as it evolved, JavaScript became a fully independent language with its own
specification called ECMAScript , and now it has no relation to Java at all.
Today, JavaScript can execute not only in the browser, but also on the server, or actually on
any device that has a special program called the JavaScript engine .
The browser has an embedded engine sometimes called a “JavaScript virtual machine”.
The terms above are good to remember because they are used in developer articles on the
internet. We’ll use them too. For instance, if “a feature X is supported by V8”, then it
probably works in Chrome and Opera.
How do engines work?
Engines are complicated. But the basics are easy.
The engine applies optimizations at each step of the process. It even watches the
compiled script as it runs, analyzes the data that flows through it, and further optimizes
the machine code based on that knowledge.
Modern JavaScript is a “safe” programming language. It does not provide low-level access
to memory or CPU, because it was initially created for browsers which do not require it.
JavaScript’s capabilities greatly depend on the environment it’s running in. For instance,
Node.js supports functions that allow JavaScript to read/write arbitrary files, perform
network requests, etc.
In-browser JavaScript can do everything related to webpage manipulation, interaction with
the user, and the webserver.
JavaScript’s abilities in the browser are limited for the sake of the user’s safety. The aim is
to prevent an evil webpage from accessing private information or harming the user’s data.
There are ways to interact with camera/microphone and other devices, but they require a
user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-
camera, observe the surroundings and send the information to the NSA .
● Different tabs/windows generally do not know about each other. Sometimes they do, for
example when one window uses JavaScript to open the other one. But even in this case,
JavaScript from one page may not access the other if they come from different sites (from
a different domain, protocol or port).
This is called the “Same Origin Policy”. To work around that, both pages must agree for
data exchange and contain a special JavaScript code that handles it. We’ll cover that in
the tutorial.
This limitation is, again, for the user’s safety. A page from http://anysite.com
which a user has opened must not be able to access another browser tab with the URL
http://gmail.com and steal information from there.
● JavaScript can easily communicate over the net to the server where the current page
came from. But its ability to receive data from other sites/domains is crippled. Though
possible, it requires explicit agreement (expressed in HTTP headers) from the remote
side. Once again, that’s a safety limitation.
https://javascript.info
https://javascript.info https://gmail.com
<script>
...
</script>
Such limits do not exist if JavaScript is used outside of the browser, for example on a server.
Modern browsers also allow plugin/extensions which may ask for extended permissions.
The syntax of JavaScript does not suit everyone’s needs. Different people want different
features.
That’s to be expected, because projects and requirements are different for everyone.
There are more. Of course, even if we use one of transpiled languages, we should also
know JavaScript to really understand what we’re doing.
Summary
● JavaScript was initially created as a browser-only language, but is now used in many
other environments as well.
● Today, JavaScript has a unique position as the most widely-adopted browser language
with full integration with HTML/CSS.
●
There are many languages that get “transpiled” to JavaScript and provide certain
features. It is recommended to take a look at them, at least briefly, after mastering
JavaScript.
Specification
The ECMA-262 specification contains the most in-depth, detailed and formalized
information about JavaScript. It defines the language.
But being that formalized, it’s difficult to understand at first. So if you need the most
trustworthy source of information about the language details, the specification is the right
place. But it’s not for everyday use.
A new specification version is released every year. In-between these releases, the latest
specification draft is at https://tc39.es/ecma262/ .
To read about new bleeding-edge features, including those that are “almost standard” (so-
called “stage 3”), see proposals at https://github.com/tc39/proposals .
Also, if you’re in developing for the browser, then there are other specs covered in the
second part of the tutorial.
Manuals
●
MDN (Mozilla) JavaScript Reference is a manual with examples and other information.
It’s great to get in-depth information about individual language functions, methods etc.
One can find it at https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference .
Although, it’s often best to use an internet search instead. Just use “MDN [term]” in the
query, e.g. https://google.com/search?q=MDN+parseInt to search for parseInt
function.
●
MSDN – Microsoft manual with a lot of information, including JavaScript (often referred to
as JScript). If one needs something specific to Internet Explorer, better go there:
http://msdn.microsoft.com/ .
Also, we can use an internet search with phrases such as “RegExp MSDN” or “RegExp
MSDN jscript”.
Compatibility tables
Code editors
A code editor is the place where programmers spend most of their time.
There are two main types of code editors: IDEs and lightweight editors. Many people use
one tool of each type.
IDE
The term IDE (Integrated Development Environment) refers to a powerful editor with
many features that usually operates on a “whole project.” As the name suggests, it’s not just
an editor, but a full-scale “development environment.”
An IDE loads the project (which can be many files), allows navigation between files,
provides autocompletion based on the whole project (not just the open file), and integrates
with a version management system (like git ), a testing environment, and other “project-
level” stuff.
If you haven’t selected an IDE yet, consider the following options:
●
Visual Studio Code (cross-platform, free).
● WebStorm (cross-platform, paid).
For Windows, there’s also “Visual Studio”, not to be confused with “Visual Studio Code”.
“Visual Studio” is a paid and mighty Windows-only editor, well-suited for the .NET platform.
It’s also good at JavaScript. There’s also a free version Visual Studio Community .
Many IDEs are paid, but have a trial period. Their cost is usually negligible compared to a
qualified developer’s salary, so just choose the best one for you.
Lightweight editors
“Lightweight editors” are not as powerful as IDEs, but they’re fast, elegant and simple.
The editors in the lists above are those that either I or my friends whom I consider good
developers have been using for a long time and are happy with.
There are other great editors in our big world. Please choose the one you like the most.
The choice of an editor, like any other tool, is individual and depends on your projects,
habits, and personal preferences.
Developer console
Code is prone to errors. You will quite likely make errors… Oh, what am I talking about? You
are absolutely going to make errors, at least if you’re a human, not a robot .
But in the browser, users don’t see errors by default. So, if something goes wrong in the
script, we won’t see what’s broken and can’t fix it.
To see errors and get a lot of other useful information about scripts, “developer tools” have
been embedded in browsers.
Most developers lean towards Chrome or Firefox for development because those browsers
have the best developer tools. Other browsers also provide developer tools, sometimes with
special features, but are usually playing “catch-up” to Chrome or Firefox. So most
developers have a “favorite” browser and switch to others if a problem is browser-specific.
Developer tools are potent; they have many features. To start, we’ll learn how to open them,
look at errors, and run JavaScript commands.
Google Chrome
Below the error message, there is a blue > symbol. It marks a “command line” where we
can type JavaScript commands. Press Enter to run them.
Now we can see errors, and that’s enough for a start. We’ll come back to developer tools
later and cover debugging more in-depth in the chapter Debugging in Chrome.
Multi-line input
Usually, when we put a line of code into the console, and then press Enter , it
executes.
To insert multiple lines, press Shift+Enter . This way one can enter long fragments
of JavaScript code.
The look & feel of them is quite similar. Once you know how to use one of these tools (you
can start with Chrome), you can easily switch to another.
Safari
Safari (Mac browser, not supported by Windows/Linux) is a little bit special here. We need to
enable the “Develop menu” first.
Open Preferences and go to the “Advanced” pane. There’s a checkbox at the bottom:
Now Cmd+Opt+C can toggle the console. Also, note that the new top menu item named
“Develop” has appeared. It has many commands and options.
Summary
● Developer tools allow us to see errors, run commands, examine variables, and much
more.
● They can be opened with F12 for most browsers on Windows. Chrome for Mac needs
Cmd+Opt+J , Safari: Cmd+Opt+C (need to enable first).
Now we have the environment ready. In the next section, we’ll get down to JavaScript.