Anatomy of a JavaScript project {

Introduction

We’re going to take a quick look at the parts of a standard JavaScript project. Mostly that’s the file structure and especially the contents of index.html and script.js.

Preparation

  1. Download the template project
  2. Unzip it
  3. Open the resulting folder in VS Code

A JavaScript project is just a folder

Key idea: a JavaScript project for the web is literally just a folder with some files in it. When that folder is put on a server the files are the website you see.

The folder structure

The different files and folders inside our template project have different roles. Let’s take a quick look at each of them and explain what it’s for:

index.html

If you look at index.html you’ll see it’s a fairly empty file. There are three important things to note here:

  1. Make sure you give your project a title in the <title></title> tag.
  2. The <link> tag is the thing telling your project to use your CSS file style.css.
  3. The <script> tags are adding three key JavaScript files to make your project work:

script.js

You will be doing almost all your work in script.js - it’s where you will write your JavaScript. By default there are three key things to care about:

  1. The “comment” at the top of the file (surrounded by /** and */) must contain the title of the project, your name, and a description of the project (including any outside help/contributions you used)
  2. function setup() is the place where you will write the JavaScript that runs when your program starts (just once)
  3. function darw() is the place where you will write the JavaScript that runs every frame of your program (over and over)

Summary

Now we’ve seen the basic files involved in a JavaScript project. Most importantly we have:

  1. The HTML file index.html that the browser checks first
  2. The CSS file style.css that determines how the webpage will look in terms of styling
  3. The assets/ folder that will contain all your images, sounds, and more
  4. The js/ folder that contains all of your JavaScript, including the libraries you’re using (p5 and p5 sound by default) and your own code in script.js

}