• 15 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 1/19/24

Get your program started with the main function

JavaScript: one language, multiple environments

One of the advantages of learning JavaScript is that it can allow you to program in several different environments. In each environment, the way in which code is executed varies slightly. Here, we will briefly explore three different environments: JSBin, web pages, and servers.

JSBin

JSBin is a great tool for trying out snippets of JavaScript: that's why I've been using it to demonstrate certain tools or principles. Let's explore how it runs code:

In JSBin, you can write code line for line in the JavaScript pane, and hit the Run button to execute. JSBin then runs through your lines of code and executes them one after the other. Therefore, order is important:

let numberOfGuests = 20;
console.log(numberOfGuests); // 20

Is not the same as:

console.log(numberOfGuests); // undefined
let numberOfGuests = 20;

JSBin is a simple environment which executes your code line by line when you hit the Run button.

Web pages

If you have been following along with the exercises so far, you have been writing JavaScript code for a web page. However, you have not yet seen how your code is interpreted behind the scenes!

In the Codevolve exercises in previous chapters, you were invited to add some code to a JavaScript file. But how was that code then executed in the built-in browser?

Well, there were some other files (to which you did not have access) which did that work for you. Firstly, there was another JavaScript file which imported your episodes array and generated elements it then added to the web page. That file was then imported by the HTML file (that tells the browser what should be on the page), and automatically ran the code.

This is pretty much how JavaScript works on the web. You write some code, save it in one or more files, and then import those files using a special tag in your HTML file. The browser then automatically runs the code in those files, generally speaking in the order in which they are imported.

Servers

It used to be that JavaScript could only be used in web pages (on the front end), but not any more! It can now also be used on the back end, managing access to certain resources.

For example, take any webmail service. You have a web page through which you can access your emails, but they are not stored in that page. They are stored in a database, and the page you use accesses that database through a server, which can be written in JavaScript!

However, servers need to be running at all times, and only specific pieces of code need to run at certain times. For example, the code that allows you to access your emails should only be executed if and when you decide to open your mail client!

In this case, an environment (like Node) coupled with specific JavaScript code, allows this to happen. The server reacts to you opening the mail client, verifies authentication, and sends your emails.

Let's recap!

In this chapter, you learned a few examples of how JavaScript is executed in a handful of environments. In the next chapter, we will start looking at conditional statements, and how you can control program flow.

Example of certificate of achievement
Example of certificate of achievement