What is JavaScript?

JavaScript is a scripting language, which is a type of programming language. A script is a series of instructions that are carried out to complete one or more automated tasks. Although scripts are programmed, they are not considered programs themselves. They are often used to carry out repetitive or complex tasks within a program (your Internet browser, for example.) A program is started on your computer, and a script is started from within the program . JavaScript runs in browsers to add functionality to websites or web apps.

Adding JavaScript to HTML

We'll use the HTML template we created in the HTML section to run JavaScript in the browser. So, how do we do this? By using <script> </script> tags! You can also use the tags to link to a JavaScript file with extension ".js". Let's start by writing directly in the html file within the script tags. We'll talk about linking to a JavaScript file later.

You could, technically, place the script tags anywhere in the html file... however, the browser reads HTML from top to bottom in sequence, so, when positioning the script tags, keep in mind that if the JavaScript code references anything below the script tags, you'll have issues, because the html being referenced isn't loaded into memory at the time the JavaScript code is interpreted. The html information has to be loaded into memory before the JavaScript code tries to retrieve the information. There are work arounds for this, but what I like to do is place the script tags below the closing <body> tag.

<!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">
</head>

<body>
<h1>HELLO WORLD!</h1>
</body>

<script>
// JavaScript goes between the script tags!
// Place script here so it loads after the body's elements
</script>

</html>

Alternatively, you can place JavaScript in it's own file (open a new text document and save it with the ".js" extension. Then, you can place your JavaScript in that file and link to it.

<script type="text/javascript" src="jquery-3.3.1.js"></script>

<!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">
</head>

<body>
<h1>HELLO WORLD!</h1>
</body>

<script type="text/javascript" src="jquery-3.3.1.js"></script>
</html>

Next, we'll talk about JavaScript syntax.

Click the next button to continue.

Next