The JavaScript language - An introduction - Part 1

 What is JavaScript?

  • JavaScript was created to make web pages interactive and dynamic.
  • Programs written in JavaScript are called scripts. These scripts can be added directly to a web page’s HTML and run automatically when the page is loaded.
  • JavaScript scripts are just plain text and do not require special preparation or compilation to work.
  • This makes JavaScript quite different from another programming language called Java.

Why is it Called JavaScript?

When JavaScript was first created, it was called “LiveScript.” However, because Java was very popular at the time, the creators decided to name it JavaScript to associate it with Java, hoping to attract attention.

Over time, JavaScript became its own unique language with a standard called ECMAScript, and it’s no longer connected to Java.

Today, JavaScript can run not only in web browsers but also on servers or any device with a special program called a JavaScript engine.

Browsers usually have a built-in engine, sometimes known as a “JavaScript virtual machine.”

Different browsers use different engines, each with its own name:

  • V8 – used in Chrome, Opera, and Edge.
  • SpiderMonkey – used in Firefox.
  • Other names include “Chakra” for Internet Explorer, “JavaScriptCore,” “Nitro,” and “SquirrelFish” for Safari.

These names are important to know since you’ll come across them in developer articles. For example, if a feature is supported by V8, it likely works in Chrome, Opera, and Edge.

How Do JavaScript Engines Work?

  • JavaScript engines can be complex, but their basic process is simple.
  • The engine reads (or "parses") the script.
  • It then converts the script into machine code.
  • The machine code runs quickly.
  • Throughout this process, the engine works to make the code run as efficiently as possible. It even monitors the code as it runs, learning how it works, and improving the machine code based on this information.

What Can In-Browser JavaScript Do?

JavaScript is considered a “safe” programming language because it doesn’t give low-level access to memory or the CPU. This design was intended since it was originally created for browsers, where such access isn't necessary.

The features JavaScript can use depend on where it’s running. For example, with Node.js, JavaScript can do things like read and write files or make network requests. But, when running in a browser, JavaScript is mainly focused on tasks like interacting with the webpage, the user, and the web server.

Here’s what in-browser JavaScript can do:

Add or modify HTML content, and change the page’s styling.

Respond to user actions like clicks, keyboard presses, or mouse movements.

Make requests to web servers, including downloading and uploading files (this is part of technologies like AJAX and COMET).

Manage cookies, prompt the user for information, and show messages.

Save small amounts of data on the client-side using "local storage."

What Can’t In-Browser JavaScript Do?

To protect the user, in-browser JavaScript has some limitations. These restrictions are meant to prevent harmful websites from accessing personal data or doing damage.


For example:

JavaScript in a webpage cannot read or write arbitrary files on a user’s computer, nor can it execute programs. It doesn't have direct access to the operating system.

Although JavaScript can work with files, the user must take specific actions like uploading a file or dragging it into the browser window.

Accessing a device’s camera or microphone requires the user's permission. A webpage can’t secretly activate the camera and spy on the user.

Different browser tabs or windows generally can’t communicate with each other. If they come from different sites (e.g., one from http://anysite.com and another from http://gmail.com), JavaScript in one tab can't access data from the other tab. This is part of the "Same Origin Policy" designed to protect user privacy.

While JavaScript can easily send requests to the server from which the current page was loaded, getting data from other websites requires special permissions and agreements between the sites.

These safety measures don’t apply when JavaScript is used outside of the browser (like on a server), where it can do more. Also, modern browsers allow plugins/extensions, which can request extra permissions from the user.

What Makes JavaScript Unique?

JavaScript has three major strengths that make it stand out:

  • Full integration with HTML and CSS – It works seamlessly with these technologies to create interactive webpages.
  • Simplicity for basic tasks – It’s easy to use for straightforward tasks and doesn’t require complex setups.
  • Universal support in major browsers – JavaScript is supported by all modern browsers, and it’s enabled by default.
  • These factors combine to make JavaScript the go-to language for building browser interfaces. In addition to its use in web browsers, JavaScript is now also used to create servers, mobile apps, and more.

Languages "Over" JavaScript

JavaScript’s syntax doesn’t suit everyone, leading to the development of alternative languages.

These languages are transpiled (converted) to JavaScript before running in the browser, thanks to modern tools.

Developers can write in these languages, which are auto-converted to JavaScript.

Examples of languages transpiled to JavaScript:

  • CoffeeScript: Offers shorter syntax for clearer code, popular with Ruby developers.
  • TypeScript: Adds strict data typing for easier complex system development (created by Microsoft).
  • Flow: Adds data typing, similar to TypeScript (developed by Facebook).
  • Dart: A standalone language by Google that can run in mobile apps and be transpiled to JavaScript.
  • Brython: A Python-to-JavaScript transpiler, allowing Python code in the browser.
  • Kotlin: A modern, concise language that targets both browsers and Node.js.

Even with these languages, understanding JavaScript is crucial to grasp how everything works.

Summary

JavaScript, once a browser-only language, is now used in various environments.

It remains the most popular language for browsers, tightly integrated with HTML/CSS.

Many languages transpile to JavaScript, offering specific features; it's helpful to explore them after mastering JavaScript.

                


Rahul Singh

As a passionate software developer, trainer, and tech blogger, I thrive on sharing knowledge and exploring the latest in technology. With a strong foundation in programming languages like C and C++, and expertise in platforms like Microsoft 365, SharePoint, and Azure, I aim to simplify complex concepts for others. My blog, Expert2Code.com, is a space where I document my learning journey, providing insights and resources for those eager to master the tech world. When I'm not coding or teaching, you'll find me reading, hiking, or discovering new tools and techniques to stay ahead in this fast-paced industry.

Post a Comment

Previous Post Next Post