JavaScript: Lesson 1: Syntax


Getting Started

To give you a basic understanding on the fundamentals of programming in JavaScript, allow me to explain two things. Sentences in JavaScript are called statements. In short statements are commands on what you want the browser to do. Constructing these statements in a way that the computer understands it and interprets it is the language syntax.

Let’s start from the top.

JavaScript code is usually defined in the HEAD part of the HTML file. Doing this will minimize the amount of errors you may encounter if you were to put it anywhere else. To start programming in JavaScript, you need to first let the browser know. You can do this with the <script> tag. This tag can either be linked to an external file where your JavaScript is located or it can contain the JavaScript between the opening and closing tags. Let’s check it out
In the HTML Document

Included in an external file

After looking at this example I am sure you noticed that the second example uses the src attribute. The value of this attribute is the location of your external file that contains your JavaScript functions. This is useful if you want to keep your JavaScript files separate from your HTML!

Note that even though you are retrieving the JavaScript functions from an external file, you still need to use the </script> tag. Also note that the extension of the external file is js!

It is also worth mentioning that you may put JavaScript in the body of the web page but it is recommended if possible to include all functions in the head of the HTML document.

External JavaScript Files

As mentioned above, you can link external JavaScript files to your HTML document via the src attribute. The external file uses the .js extension. So what does the external file look like? Since you declare the file as JavaScript in the HTML and since the file has the .js extension, you do not need to make any other sort of declarations inside the JavaScript file. Let’s look at an example:
The Javascript File (myJavaScript.js)

The HTML Document

The Javascript File (myJavaScript.js)

The Result

Benefit of using an External File

We recommend using an external file because it is beneficial in a couple ways:

  • Keeps your HTML neater and easier to read
  • Keeps all your JavaScript statements in one place!
  • Includes the same JavaScript files in multiple HTML documents!

The Semicolon ( ; )

As with many languages, the semicolon separates JavaScript statements, however it is only necessary to use semicolons when the statements are on the same line, such as:

Reserved words

JavaScript reserved words, also known as keywords are words that are used for specific JavaScript functionality. Each reserved word serves a specific purpose. For example, the JavaScript keyword var is the key word for declaring a variable. Don’t worry too much about it now, at this point I just want you to be aware of what they are.


  • Now have a basic understanding of JavaScript Syntax
  • Statements are short sentences that tell the browser what to do
  • You can add JavaScript in an external file or in the <head> or <body> part of the HTML document
  • Putting your JavaScript in a separate .js file allows you to access it one place
  • Semicolons are only needed when separating statements on a single line

Next Lesson

Next we will cover JavaScript Output. Here we will create our first ‘Hello World’ page and apply what we have learned!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.