Setting Up JavaScript
Getting a Code Editor
First of all, we will install a Code Editor, or a program that we can type our JavaScript into. If you already have a Code Editor like a Text Editor or IDE, just skip this part.
One of the easiest to use Code Editors around is Sublime Text. We will install that too. To do so, visit the Sublime Text Download Page and select the download file for your operating system. Then, run through the Setup Wizard and you'll have Sublime Text installed!
Linking Our JavaScript Code In HTML
There are two main ways to run JavaScript in a browser, and they both involve linking our having the JavaScript code in your HTML file.
The first option is putting your JavaScript code between two <script>
tags. For example, we could have
<script>
alert("Hello World!")
</script>
in our code. We just simply embedded our JavaScript code into the HTML file. This is the first main way to write JavaScript code, but it isn't very widely used because it can be hard to find your code.
The second and more preferable option is writing your JavaScript code in an external .js
file, which is usually in the same directory, or folder, as the index.html
file. Then, we can simply reference the JavaScript file by typing:
<script src="path/to/javascript/filename.js"></script>
A benefit of the above method is that we can make edits easier and make our JavaScript code more organized. Using this method, we can also organize our JavaScript into different files.
It is up to you for what method you choose, but in this JavaScript course, we will use the second method, or the src="script.js"
method.