Skip to content

Introduction

Jacob Mai Peng edited this page Jun 9, 2021 · 4 revisions

Running JavaScript: Hello, World!

First, we'll learn how to run our JavaScript code. There are two main places that JS typically executes: on the client and the server.

Client-side JavaScript

Create a file named hello.html wherever you like and give it the following contents:

<script>
    console.log("Hello from the client!");
</script>

Notice that we've created an HTML file and embedded some JS within a <script> tag. Open the file using your favourite web browser. You should see a blank screen. Now if you open your Developer Console, you should see the message "Hello from the client!" printed in your console. In real websites, your HTML will be sent to users' computers and the included JavaScript will execute in their web browsers.

In practice we usually won't write JavaScript directly in our HTML files, but the HTML files will link to the .js JavaScript files that we write. More on this later.

Another thing worth noting is that the developer console is an immensely useful tool for experimenting and debugging. It behaves similarly to the Python REPL, executing the code you enter and immediately returning the results. In future sections, I highly recommend copying and pasting the code snippets into your developer console to see how they behave.

Server-side JavaScript

Create a file named hello.js with the following contents:

console.log("Hello from the server!");

Now, with a terminal window in the same directory as your file, run your script with the following command:

$ node hello.js

You should see the message "Hello from the server!" printed to the screen. Though it may seem simple, this method of writing code and executing it with the node command is exactly how we write server-side code. Later, we'll write code that accepts connections over the internet and starts behaving like a proper web server.

Congratulations, you've just programmed JavaScript on both the client and server!

ECMAScript? ES6?

One of the biggest hurdles to learning JavaScript as a new developer is that the language has undergone significant evolutions since its inception. You can read about it here (totally optional). When reading about JS on the internet, you may come across the following terms:

  • When people say "ECMAScript", they almost always effectively mean JavaScript.
  • "ES6" is the same thing as "ES2015", which is again the same thing as "ECMAScript 6". Both of these terms refer to a relatively recent version of JavaScript.
    • You should generally prefer using ES6 techniques to older techniques wherever possible (let/const instead of var - more on this later!)

Clone this wiki locally