Coding Basic Start With Hints and Tips
Coding Basic Start With Hints and Tips
Learning outcomes
The standard website consists of HTML templates, CSS styles, and JavaScript
scripts.
HTML presents static content. CSS is responsible for the modern and smooth
view of our HTML elements, while JavaScript (JS) is a programming language
enabling us to create dynamic content on websites.
Visual Studio Code is the most popular code editor for web application
development (and for many other applications like data science applications
written in Python). It’s available to download for free under a standard MIT
license. It contains numerous extensions which heavily improve the coding
experience.
This lesson guides through the installation process of Visual Studio Code and
presents a basic website structure. Examples of a standard web application
are presented and explained.
Variables are containers for data. Before a value is assigned to a variable, it’s
like an empty whiteboard. However, the caveat is that we have different
types of whiteboards for different kinds of data. Imagine we have
whiteboards on which only numbers can be written and those on which only
character strings can be written.
Variables can have various types. In this lesson, we’ll focus on numeric
types: Numbers and decimals. We’ll go through basic mathematical
operations such as addition, subtraction, multiplication and division.
There are some other useful operators like type, logical and bitwise
operators. We’ll also mention some examples.
To use the Snipping Tool when you have a mouse and a keyboard:
1. Press Windows logo key + Shift + S. The desktop will darken while you
select an area for your screenshot.
Getting started with Visual
Studio Code
Open a folder
File > Open Folder (Ctrl+K Ctrl+O)
File Explorer
View > Explorer (Ctrl+Shift+E)
Search view
View > Search (Ctrl+Shift+F)
Source Control
View > Source Control (SCM) (Ctrl+Shift+G)
Run and Debug
View > Run (Ctrl+Shift+D)
Extensions view
View > Extensions (Ctrl+Shift+X)
Open the Command Palette.
View > Command Palette... (Ctrl+Shift+P)
Output panel
View > Output (Ctrl+Shift+U)
Debug Console
View > Debug Console (Ctrl+Shift+Y)
Problems panel
View > Problems (Ctrl+Shift+M)
Integrated Terminal
View > Terminal (Ctrl+`)
Create a new file
File > New File (Ctrl+N)
Save a file
File > Save (Ctrl+S)
Auto Save
File > Auto Save
Run
Run > Start Debugging (F5)
Programming language extensions
Python - IntelliSense, linting, debugging, code formatting, refactoring,
and more.
Live Preview - Hosts a local server to preview your webpages.
Zoom
Zoom out (Ctrl+-)
Zoom in (Ctrl+=)
Customize your editor with color themes.
File > Preferences > Theme > Color Theme (Ctrl+K Ctrl+T)
Extentions
https://code.visualstudio.com/docs/nodejs/extensions#:~:text=You%20can%20find%20JavaScript
%20extensions,you%20can%20search%20for%20Node
HTML5 document
HTML is a standard language for creating web pages. HTML stands for
HyperText Markup Language. It’s responsible for putting small bricks on our
side, from which we use more advanced technologies to create a fantastic
website.
In this section, we’ll go through the structure of the basic HTML5 file. HTML5
is the most recent version of HTML language.
<!DOCTYPE html>
<html>
<head>
<title>App</title>
</head>
<body>
Hello world!
</body>
</html>
<body>
</body>
</html>
Copy
Right now, this file only contains HTML markup. Let’s say we
would like to add the following JavaScript code to the document:
let d = new Date();
alert("Today's date is " + d);
Copy
This will enable the webpage to display an alert with the
current date regardless of when the user loads the site.
In order to achieve this, we will add a <script> tag along with
some JavaScript code into the HTML file.
To begin with, we’ll add the JavaScript code between the <head>
tags, signalling the browser to run the JavaScript script before
loading in the rest of the page. We can add the JavaScript below
the <title> tags, for instance, as shown below:
index.html
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
<script>
let d = new Date();
alert("Today's date is " + d);
</script>
</head>
<body>
</body>
</html>
Copy
Once you load the page, you will receive an alert similar to
this:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
</head>
<body>
<script>
let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
</script>
</body>
</html>
Copy
The output for the above HTML document loaded through a web
browser would look similar to the following:
Scripts that are small or that run only on one page can work
fine within an HTML file, but for larger scripts or scripts that
will be used on many pages, it is not a very effective solution
because including it can become unwieldy or difficult to read
and understand. In the next section, we’ll go over how to handle
a separate JavaScript file in your HTML document.
Working with a Separate JavaScript File
In order to accommodate larger scripts or scripts that will be
used across several pages, JavaScript code generally lives in
one or more js files that are referenced within HTML documents,
similarly to how external assets like CSS are referenced.
The benefits of using a separate JavaScript file include:
Separating the HTML markup and JavaScript code to make both
more straightforward
Separate files makes maintenance easier
When JavaScript files are cached, pages load more quickly
To demonstrate how to connect a JavaScript document to an HTML
document, let’s create a small web project. It will consist of
script.js in the js/ directory, style.css in the css/ directory,
and a main index.html in the root of the project.
project/
├── css/
| └── style.css
├── js/
| └── script.js
└── index.html
We can start with our previous HTML template from the section
above:
index.html
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
</head>
<body>
</body>
</html>
Copy
Now, let’s move our JavaScript code that will show the date as
an <h1> header to the script.js file:
script.js
let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
Copy
We can add a reference to this script to the <body> section, with
the following line of code:
<script src="js/script.js"></script>
Copy
The <script> tag is pointing to the script.js file in the js/
directory of our web project.
Let’s consider this line in the context of our HTML file, in
this case, within the <body> section:
index.html
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
</head>
<body>
<script src="js/script.js"></script>
</body>
</html>
Copy
Finally, let’s also edit the style.css file by adding a
background color and style to the <h1> header:
style.css
body {
background-color: #0080ff;
}
h1 {
color: #fff;
font-family: Arial, Helvetica, sans-serif;
}
Copy
We can reference that CSS file within the <head> section of our
HTML document:
index.html
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="js/script.js"></script>
</body>
</html>
Copy
Now, with the JavaScript and CSS in place we can load the
index.html page into the web browser of our choice. We should see
a page that looks similar to the following:
From here, you can learn how to work with the JavaScript
Developer Console and how to write comments in JavaScript.