Introduction to JavaScript

What is Javascript?

JavaScript was originally created in order to make web-pages “live”. Programs in this language are called scripts . In the browser, they connect directly to HTML and, as soon as the page loads, they are immediately executed.

JavaScript programs are plain text . They do not require any special training.

In this regard, JavaScript is very different from another language, which is called Java .

Why Javascript?

When the JavaScript language was created, it originally had another name: “LiveScript”. But then the Java language was very popular, and marketers decided that a similar name would make the new language more popular.

It was planned that JavaScript would be such a “younger brother” Java. However, history decreed in its own way, JavaScript has grown greatly, and now it is a completely independent language, with its specification, which is called ECMAScript , and has no relation to Java.

He has many features that complicate the development, but in the course of the textbook, we’ll deal with them.

JavaScript can be executed not only in the browser, but anywhere, you only need a special program – an interpreter . The process of executing the script is called “interpretation”.

Compilation and interpretation for programmers

To execute programs, no matter in what language, there are two ways: “compilation” and “interpretation”.

  • Compilation is when the source code of a program, with the help of a special tool, another program called “compiler”, is converted into another language, as a rule – into machine code. This machine code is then distributed and started. In this case, the source code of the program remains with the developer.
  • Interpretation is when the source code of a program is received by another tool, which is called an “interpreter,” and executes it “as is”. In this case, the source code itself (script) is distributed. This approach is used in browsers for javascript.

Modern interpreters convert JavaScript into machine code or close to it, optimize and then execute before execution. And even at runtime they try to optimize. Therefore, JavaScript runs very fast.

A JavaScript interpreter is built into all major browsers, which is why they can execute scripts on the page. But, of course, JavaScript can be used not only in the browser. This is a full-fledged language, programs on which you can run on the server, and even in the washing machine, if the corresponding interpreter is installed in it.

Talk about browsers

Later in this chapter we talk about the possibilities and limitations of JavaScript in the context of the browser.

What can Javascript do?

Modern JavaScript is a “secure” general-purpose programming language. It does not provide low-level means of working with memory, processor, as it was originally focused on browsers in which it is not required.

As for the other features, they depend on the environment in which JavaScript is running. In the browser, JavaScript can do everything related to page manipulation, interaction with the visitor and, to some extent, with the server:

  • Create new HTML tags, delete existing ones, change element styles, hide, show elements, etc.
  • Respond to the actions of the visitor, handle mouse clicks, move the cursor, pressing the keyboard, etc.
  • Send requests to the server and upload data without reloading the page (this technology is called “AJAX”).
  • Get and set cookies, request data, display messages …
  • …and many many others!

What doesn’t do Javascript?

JavaScript is a fast and powerful language, but the browser imposes some restrictions on its execution …

This is done for the safety of users so that an attacker could not get personal data using JavaScript or somehow harm the user’s computer.

There are no such restrictions where JavaScript is used outside the browser, for example on a server. In addition, modern browsers provide their own mechanisms for installing plug-ins and extensions that have advanced features but require special installation actions from the user.

In Most browser – JavaScript features are limited to the current window and page in most of the browsers.

Javascript Browser Features
  • JavaScript cannot read / write arbitrary files on the hard disk, copy them, or call programs. It does not have direct access to the operating system.Modern browsers can work with files, but this feature is limited to a dedicated directory – “sandbox” . Opportunities for accessing devices are also being worked out in modern standards and are partly available in some browsers.
  • JavaScript running in the same tab cannot communicate with other tabs and windows, unless it opens this window or several tabs from the same source (same domain, port, protocol).There are ways to get around this, and they are covered in the textbook, but they require special code for both documents that are in different tabs or windows. Without it, for security reasons, you cannot get from one tab to another using JavaScript.
  • From JavaScript, you can easily send requests to the server from which the page came. A request for another domain is also possible, but less convenient, since there are security restrictions here too.

What is the uniqueness of Javascript?

There are at least three great JavaScript features:

  • Full HTML / CSS integration.
  • Simple things are done simply.
  • Supported by all common browsers and enabled by default.

These three things at the same time no more in any browser technology.

Therefore, JavaScript is the most common means of creating browser interfaces.

Development trends

Before you plan to explore a new technology, it is useful to get acquainted with its development and prospects. Here in JavaScript everything is more than good.

HTML 5

HTML 5 is an evolution of the HTML standard that adds new tags and, more importantly, a number of new features for browsers.

Here are some examples:

  • Reading / writing files to disk (in a special “sandbox”, that is, not any).
  • Built-in browser database that allows you to store data on the user’s computer.
  • Multi-tasking with simultaneous use of multiple processor cores.
  • Play video / audio without Flash.
  • 2D and 3D drawing with hardware support, as in modern games.

Many HTML5 features are still in development, but browsers are gradually starting to support them.

Trend: JavaScript is becoming more and more powerful and browser capabilities are growing towards desktop applications.

ECMAScript 6

The JavaScript language itself is improving. The modern ECMAScript 5 standard includes new features for development, ECMAScript 6 will be a step forward in improving the language syntax.

Modern browsers improve their engines to increase the speed of JavaScript execution, fix bugs and try to follow the standards.

Trend: JavaScript is becoming faster and more stable, new features are added to the language.

It is very important that the new standards HTML5 and ECMAScript retain maximum compatibility with previous versions. This avoids trouble with existing applications.

However, a small problem with the “super-modern stuff” is still there. Sometimes browsers try to include new features that are not yet fully described in the standard, but are so interesting that developers simply cannot wait.

However, over time, the standard is changing and browsers have to adapt to it, which can lead to errors in the already written, based on the old implementation, JavaScript code. Therefore, you should think twice before putting into practice such “super-new” solutions.

In this case, all browsers converge to the standard, and the differences between them are already much smaller than just a few years ago.

Trend: everything goes to full compatibility with the standard.

Alternative browser technologies

Other technologies are used on the pages along with JavaScript. Bundling with them can help achieve more interesting results in those places where browser-based JavaScript is not as good as we would like.

Java

Java is a general purpose language, you can write a variety of programs on it. For Internet pages there is a special opportunity – writing applets .

An applet is a Java program that can be connected to HTML using a tag applet. It looks like this:

<applet code="BTApplet.class" codebase="/files/tutorial/intro/alt/">
  <param name="nodes" value="50,30,70,20,40,60,80,35,65,75,85,90">
  <param name="root" value="50">
</applet>

This tag loads a Java program from a file BTApplet.classand executes it with parameters param. The applet is executed in a separate part of the page, in a rectangular “container”. All user actions inside it are processed by the applet. The container, however, may be hidden if the applet has nothing to show.

Of course, to do this, the Java runtime must be installed and enabled on your computer, including the browser plugin. In addition, the applet must be signed by the publisher’s certificate (in the example above, an unsigned applet), otherwise Java will block it.

How can we, JavaScript developers, be interested in Java?

First of all, the fact that a signed Java applet can be the same as a regular program installed on a visitor’s computer. Of course, this will require the user’s consent when opening such an applet.Virtues

  • Java can do everything on behalf of the visitor, just like an installed program. Potentially dangerous actions require a signed applet and user consent.

Disadvantages

  • Java takes more time to load.
  • The Java runtime environment, including the browser plugin, must be installed on the visitor’s computer and enabled.
  • The Java applet is not integrated with the HTML page, but is executed separately. But it can call javascript functions.

Browser Plugins and Extensions

All modern browsers provide the ability to write plugins. For this, you can use both JavaScript (Chrome, Opera, Firefox) and C (ActiveX for Internet Explorer).

These plug-ins can both display content in a special format (a plug-in for playing music, for displaying PDF), or interact with a page.

As in the situation with a Java applet, they have ample opportunities, but the visitor will deliver them if he trusts you.

Adobe flash

Adobe Flash is a cross-browser platform for multimedia applications, animations, audio and video.

A flash movie is a compiled program written in ActionScript. It can be connected to an HTML page and run in a rectangular container.

First of all, Flash is useful in that it allows cross-browser work with a microphone, camera, clipboard, and also supports advanced features for working with network connections.Virtues

  • Sockets, UDP for P2P and other advanced features for working with network connections
  • Multimedia support: images, audio, video. Work with a webcam and microphone.

Disadvantages

  • Flash must be installed and enabled. And on some devices it is not supported at all.
  • Flash is not integrated with the HTML page, but is executed separately.
  • There are security restrictions, however, they are slightly different than in JavaScript.

You can call JavaScript from Flash and vice versa, which is why sites usually use JavaScript, and where it cannot cope, you can think about Flash.

Languages ​​over Javascript

The syntax of JavaScript does not suit everyone: one seems too free, others, on the contrary, too limited, others want to add additional features to the language that are not in the standard.

This is normal, because the requirements and projects are different for everyone.

In recent years, there have been many languages ​​that add various features “on top of” JavaScript, and for launching in the browser — with the help of special tools “translators” are transformed into ordinary JavaScript code.

This conversion takes place automatically and is completely transparent, while there is practically no inconvenience in development and debugging.

At the same time, different languages ​​look different and add completely different things:

  • Language CoffeeScript – a “syntactic sugar” on top of JavaScript. It focuses on greater clarity and brevity of code. As a rule, Ruby programmers are especially fond of it.
  • Language TypeScript focused on the addition of strict data typing. It is designed to simplify the development and support of large systems. It is developed by Microsoft.
  • The Dart language is interesting in that it is not only translated into JavaScript, like other languages, but also has its own independent execution environment, which gives it a number of possibilities and is available for embedding in applications (outside the browser). It is developed by Google.

ES6 and ES7

There are also translators that take code that uses the capabilities of future JavaScript standards, and transform it into an older version that all browsers understand.

For example, babeljs .

Because of this, we can use many of the possibilities of the future today.

Conclusion

JavaScript is unique due to its full integration with HTML / CSS. It works for almost all visitors.

But a good JavaScript programmer should not forget about other technologies.

After all, our goal is to create good applications, and here Flash, Java and browser extensions have their own unique features that can be used with JavaScript.

As for CoffeeScript, TypeScript, and other languages ​​built on JavaScript, they can be very useful. It is recommended to see them, at least in general terms, but, of course, after mastering JavaScript itself.

Also Read: Javascript Interview Questions

Leave a Comment