WD T02 - Your First Computer Program
WD T02 - Your First Computer Program
In this task, you are introduced to the JavaScript programming language. This is a
scripting language that allows you to create dynamic websites. In this task, you will
learn what JavaScript is. You will also learn the basics of creating your first program
using JavaScript! In doing so, you will become familiar with the structure of a
JavaScript program.
Remember that with our courses, you’re not alone! You can contact an expert code reviewer to
get support on any aspect of your course.
Our expert code reviewers are happy to offer you support that is tailored to your individual
career or education needs. Do not hesitate to ask a question or for additional support!
INTRODUCTION TO JAVASCRIPT
JavaScript is often used as a front-end scripting language. However, as you will learn
later, it can also be used for server-side programming.
HTML, CSS and JavaScript are the three core technologies used for front-end web
development. HTML and CSS describe what content is shown on a webpage and
what that content looks like. JavaScript is used to make the webpage do things.
JavaScript is used to change web pages from static web pages (that always look the
same) to dynamic web pages (pages that can dynamically change on the browser).
JAVASCRIPT BACKGROUND
Before you start learning to use JavaScript, it is important to first cover some theory
and background related to JavaScript.
ECMAScript
ES6 has introduced many changes to JavaScript (which basically is the same thing as
ECMAScript) that improve the programming language greatly. Besides ES6, though,
there are also already versions ES7 and ES8. This is because, since the release of ES6,
the ECMA has decided to release updates to ECMAScript every year. All versions of
ECMAScript after ES6 should, therefore, have fewer revisions than ES6.
When you start coding in JavaScript, you may notice that there are often different
ways of writing the same code. The reason for this is often that some code will be
written using the ‘older’ version of JavaScript while other code will be written using
the changes to the language that have been introduced since ES6. The updates to
JavaScript since ES6 will be highlighted in your tasks.
All modern browsers offer built-in support for JavaScript (you’re using it without even
realising it). Browsers have a built-in console that can be used for debugging web
pages. The functionality of the console may differ slightly based on the browser you
use. In this task, we will be using Chrome’s DevTools Console. To access this tool, open
Chrome and right-click → Inspect, or press either Ctrl+Shift+J if you are using
Windows / Linux or Cmd+Opt+J if you are using Mac. You should see something
similar to the screen shown in the image below. The console may already display
some messages. You can clear these by right-clicking on the console and then
selecting “Clear console.”
You can input your JavaScript code directly into this console to test and debug it. To
write information to the console, we use the instruction:
The above is a line of JavaScript code. It instructs your computer to log (or write) the
text in the quotation marks to the console.
Try this:
1. Open the Console.
2. Type the code console.log(“Hello World!!”); into the console.
3. Press enter.
If you typed everything correctly, “Hello World!!” should be shown in the console as
shown below:
If you haven’t typed the instruction exactly as shown (for example if you forgot to add
the quotation marks, or you used capital letters where it should have been lowercase
letters, or you spelt “console” incorrectly) you may get an error. See an example of an
error below:
Like all programming languages, JavaScript has syntax rules that must be followed
closely. Otherwise, your instructions will not be correctly interpreted and executed.
SYNTAX RULES
All programming languages have syntax rules. Syntax is the "spelling and grammar
rules" of a programming language and determines how you write correct,
well-formed statements.
A common syntax error you could make above is forgetting to add a closing quotation
mark ("). Remember that all opening quotation marks (") require a closing one!
Another common syntax error that you could make above is by forgetting to add a
closing bracket ‘)’. Remember that all opening brackets ‘(’ require a matching closing
one!
Any program you write must be exactly correct. All code is case-sensitive. This means
that ‘Console’ is not the same as ‘console’. If you enter an invalid JavaScript command,
misspell a command or misplace a punctuation mark, you will get a syntax error
when trying to run your program.
Errors appear in the JavaScript console when you try to run a program and it fails. Be
sure to read all errors carefully to discover what the problem is. Error reports will even
show you what line of your program had an error. The process of resolving errors in
code is known as debugging.
Did you know that the first computer “bug” was named after a real bug? Yes, you read that
right! Although the term “bug”, in the sense of a technical error, was first coined by Thomas
Edison in 1878, it was only 60 years later that someone else popularised the term.
In 1947, Grace Hopper, a US Navy admiral, recorded the first computer ‘bug’ in her logbook as
she was working on a Mark II computer. A moth was discovered stuck in a relay and thus
hindering the operation. She proceeded to remove the moth, thereby ‘debugging’ the
system, and taped it in her logbook. In her notes, she wrote, “First actual case of bug being
found.”
The code above will always produce the same output, but sometimes we want some
aspects of the output to change each time the code is run. For example, imagine that
you wanted to write the code that prints the message “Welcome to my web page
Tom” to the console but instead of using the name “Tom” every time, you’d like to be
able to change the name based on who is currently viewing your web page. To do this,
we need a variable.
VARIABLES
A script is basically a set of instructions that are interpreted to tell the computer what
to do in order to accomplish a certain task. Programs usually process data that is
somehow input into the program and output the results of the processing. This data
must be stored somewhere so that it can be used and processed by the instructions
we code. When coding, we use variables to store the data we need to manipulate.
DECLARING VARIABLES
Before we can use variables in our code, we need to declare them. To declare a
variable is to assign it a storage space in memory and give it a name for us to
reference it with. This tells JavaScript that we want to set aside a chunk of space in the
computer's memory for our program to use.
In JavaScript we use the following format to create a variable and assign a value to it:
2. After that, you declare the name of the variable. You can name a variable
anything you like, as long as the name:
a. Contains only letters, numbers and underscores. All other characters are
prohibited from use in variable names, including spaces. “my name”
would thus not be an acceptable variable name, whereas “my_Name” or
“myName” would be acceptable.
If you follow these rules, you can call your variables whatever you want. It is, however,
good practice to give your variables meaningful names.
To assign a value to a variable, you need to use the assignment operator. This is the
equal-to sign (=) we usually use in maths. It takes the value on the right-hand side of
the = and stores it in the variable on the left-hand side. Finally, we finish the line with a
semicolon (;). For example, consider the line of JavaScript code below:
That statement would cause your computer to create an area in memory (i.e. a
variable) called “myName” and put the value “Tom” into that area in memory.
It is important that you always put the value you want to assign (or put into) your
variable on the right-hand side of the assignment operator (=).
Try this:
You have now successfully used a variable! In the example above, “myName” is
referred to as a variable because the value stored in the position in memory named
“myName” will vary/change throughout our program. You will learn how to get values
to store in variables from users and other sources in later tasks.
Variables store data and the type of data that is stored by a variable is intuitively called
the data type.
We can also ask the user to give us the value for a variable. We do this using prompt().
Have a look below:
let myName = prompt("What is your name?"); // Asks the user their name
console.log("Hi there, " + myName); // Will show "Hi there, [myName]"
The code above prompts the user to input their name, and then logs “Hi there, “ and
their name to the console.
All the code we have written so far has been written directly into Google’s DevTools
Console. As soon as you close your browser though, all the code you wrote in the
console will be lost. To write JavaScript code that you can save locally and reuse for
your websites, you’ll need to create JavaScript files. In this course, we will be using
Visual Studio Code to write and save files from now on. To set up Visual Studio Code,
follow the step-by-step guide in this task file.
Once everything is installed and set up, simply create a new file and save it with the .js
extension.
Getting to grips with JavaScript takes practice. You will make mistakes in this task.
This is completely to be expected as you learn the keywords and syntax rules of this
programming language. It is vital that you learn to debug your code. To help with this,
remember that you can:
● Use either the JavaScript console or Visual Studio Code (or another editor of
your choice) to execute and debug JavaScript in the next few tasks. To see how
to configure Visual Studio Code for debugging and executing JavaScript, please
see the additional reading that accompanies this task.
● Remember that if you really get stuck, you can contact an expert code reviewer
for help.
Compulsory Task 1
Follow these steps:
Compulsory Task 2
Think that the content of this task, or this course as a whole, can be improved, or think we’ve
done a good job?