Javascript

Share

What is Javascript?

Javascript is the programming language used by websites to program your web browser. Whenever you access a webpage that has Javascript in it, the Javascript is interpreted as a program by the web browser, so it can do all sorts of extremely basic things that HTML and CSS can't do by default, like display a menu, letting you drop things onto a page, showing image previews, loading parts of a page asynchronously, etc.

Javascript is also used in web servers for some reason.

Fun fact: Javascript has nothing to do with Java! I've read that the name comes from the fact that Java, another programming language, was popular at the time. It's a good idea to pretend you don't know this to make programmers angry!

Example of Javascript Code

Here's how you set a in Javascript:

// no strict types here
const arr = [1, 'foo', { bar: null }];
for(it of arr) {
    // this doesn't error because Javascript.
    console.log(it + 1);
}

Runtime Environment

It's worth noting that there are two completely different runtime environments for Javascript: the web browser and Node.

When Javascript is run by a web browser, it has access to a global variable document which lets it access the webpage's Document Object Model (DOM). This lets Javascript code add, remove, and change HTML elements, effectively changing what a user sees on the screen, in their web browser.

When Javascript runs in a web server, there is no web browser. Its runtime is provided by Node. Consequently there is no way to modify the DOM, because it's not running in a user's computer.

More critically, web browsers don't provide an API for Javascript code running inside a web browser to access the user's files. There is absolutely no way for the Javascript of a webpage to delete your files, or read them without your permission. Even though any program can delete all your files, the Javascript programs can't do that. That's because they don't have direct access. They are running on top of the web browser, not on top of an operating system. The only way for a program to read your files is if you literally drop them into the web browser.

On the other hand, a program that is running on top of Node can access the web server's files without a problem.

A third case exists: Electron. Electron lets developers build applications for desktop using web technologies, which is fancy way of saying they just ship an entire web browser to display a single website. Many popular websites use Electron for their "apps," such as Discord, Spotify, etc. Electron applications have a Node side and a web browser side.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *