Javascript

Javascript Introduction

Welcome to our guide to Javascript. For this guide, we will assume you have some understanding of HTML and CSS. Javascript is the language we use to add interactivity to the web. Javascript comes in two major forms. Although others exist, these two are the most popular:

  • A frontend language, which is included in HTML files, that adds interactivity to the website.
  • A backend language, for writing web servers, in the form of Node.JS.

What is Javascript?

Javascript Logo

Javascript is a language based off the ECMAScript standard. Javascript is a weakly typed language, meaning you don't have to define types our output types when writing code in it. Javascript will understand what types you are trying to use, based on the context they are used in.

Javascript has two main implementations - it comes built in to your browser, but different browsers have different support for new Javascript features. Therefore, some new Javascript features are not supported on all browsers. The second implementation is via Node.JS. Again, Node.JS may not support all new Javascript features immediately, but it is usually installed on servers, rather than in browsers.

What can I do with Javascript?

Javascript lets you do a lot of things. For the frontend, some examples of this includes:

  • Add or change CSS in an HTML document.
  • Create new HTML elements programatically.
  • Track what a user is doing, and react - i.e., do something when a user clicks.
  • Change or delete HTML tags, i.e. change the classes an element has.
  • Store data in objects and arrays.

On the backend, things are different. You can do things like:

  • Create entire websites, and code what happens at certain URLs.
  • Build servers that users can connect to.
  • Build live APIs and Web Sockets that users can connect to.
  • Handle compression of files for a faster experience.
  • Manipulate data for storage in databases.

Adding Javascript to Websites

There are a few ways to include Javascript in a website. The best way to include Javascript is in an external file.

  • Inline scripts, that sit within HTML documents.
  • In a separate file, included in HTML documents via the script tag.
  • Within HTML elements, using attributes.

In an inline script

This is useful for Javascript you can't include easily in an external file. It looks like this:

html Copy
<script type="text/javascript"> // Javascript goes here </script>

In a separate file

This is the best way to include Javascript. You can link to an external Javascript file via the src attribute:

html Copy
<script src="index.js"></script>

In a an HTML tag

This is discouraged, but you may see something like this in HTML documents sometimes:

html Copy
<button onclick="//javascript here"></button>

Using Javascript in Node.JS

As mentioned, Javascript is also frequently used as a backend language. For this to work, make sure you have installed Node.JS via this link.

After that, you can create standalone Javascript files, i.e. index.js, and run them from the command line. You can do this by navigating to the folder your file is in, and running the node command:

shell Copy
node index.js
Last Updated Saturday, 12 June 2021
Click to Subscribe Subscribed

Subscribe

Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email