Introducing JavaScript
A look at the core language
Dr Graeme StuartA look at the core language
Dr Graeme Stuart
“Java is to JavaScript as car is to carpet”
- Chris Heilmann

“ECMAScript was always an unwanted trade name that sounds like a skin disease.” - Brendan Eich
There are several methods of the console API that we can use.
It can be accessed via a script to log messages. Typically these messages are for the developer. We can set the visual level of our messages.
| |
We can optionally log an error with
console.assert.
| |
We can display data with
console.table.
| |
We can time execution with
console.timeandconsole.timeEnd.
| |
The console in the browser can be used directly as a REPL interface.
TL;DR: use
constby default. If you need reassignment, uselet.
const
| |
let
| |
var
| |
Errors are good, declaring variables in the right way is good practice.
Data types in Javascript are pretty simple. The basic data types are Boolean, Number and String.
| |
There is also a Bigint data type for when values greater than 253 are reasonably expected.
| |
As a dynamically typed language, JavaScript attaches type information to the data, not the variable.
Reassigning values to variables creates new objects in memory and old values are garbage collected.
| |
Relational, equality and logical operators return boolean values.
| |
Create Boolean values with literals, the Boolean function or double NOT operator.
| |
Boolean values are fundamental to
ifandwhilestatements and e.g. theArray.filter()function,
All numbers are floating point. Maths is pretty easy and obvious.
| |
The Math namespace object allows you to do more complicated stuff.
| |
See also: Math.min, Math.max, Math.pow, Math.trunc, Math.sin, Math.cos, Math.sign, Math.random and many moreā¦
String literals are specified by single or double quotes.
Basic string manipulation can be achieved with concatenation, though it is annoying and difficult to read.
| |
String interpolation with template literals uses backticks and expressions can be included with a dollar sign and curly braces.
| |
String interpolation is intuitive and elegant as well as faster and better, so use it..
| |
Strings have loads of useful methods like
includes,
replace,
trim and
split.
Use if...else to execute a block conditionally.
Whatever you pass into the
ifclause will be converted to boolean. If its not truthy, then the block will be skipped.
The
elseclause is optional. Furtherelse if()clauses can be added before the finalelse.
| |
| |
| |
The ternary operator is often useful as a shorthand conditional statement.
switch statements are good when there are many options.
| |
Be careful though, execution will pass through all the subsequent options unless and until you include the
breakstatement.
Repeating code multiple times.
Similar to Java or C, for loops allow things to be repeated a number of times.
| |
A while loop will only execute while a condition is met.
| |
A do…while loop will always execute once before the condition is checked.
| |
Functions can help to avoid repetition and make code more readable and more maintainable.
the
functionkeyword is followed by a comma-separated argument list in parentheses and a code block with an optionalreturnstatement.
| |
A newer syntax (ECMAScript 2015) for more compact functions using arrow (=>) notation.
| |
Simple one-line arrow functions can be very compact.
| |
Arrays are ordered collections of data.
Create them using square bracket notation, they can contain anything.
| |
They are indexed with integers (beginning with zero).
| |
Or use the at method which also allows negative indices, counting from the end at -1.
| |
They have built-in methods for basic mutations.
| |
Some really useful methods of the Array class take callback functions as the main argument.
This is a powerful tool when more complex transformations are needed.
forEachsimply executes the callback for each element.
| |
filterwill return a newArrayincluding only those elements that return true when passed through the provided function.
| |
mapReturns a newArrayin which each element has been converted by the provided function.
| |
Built-in iterables such as
StringandArrayfollow the iterable protocol. This allows them to define a sequence which can be iterated using afor...ofloop.
| |
Example with an Array
| |
This is a great way to iterate over things like elements extracted from the DOM.
Objects can be treated as key/value pairs. Though they are far more than that.
Objects are defined with curly braces containing comma-separated key/value pairs. Each key/value pair is separated with a colon.
| |
They can be modified using square bracket notation.
| |
Though dot notation is more convenient, it is limited (e.g. no spaces in keys).
| |
for...inloops iterate over the key values. This is the same for arrays (where the keys are integers).
| |
A map is an object type which holds key-value pairs. It is often more efficient than an object.
Create a map with the
newkeyword and use theset()method to store values against keys.
| |
Extract values using the
get()method.
| |
Map objects are iterable, yielding key-value pairs.
| |
Test if items exist with the
has()method.
| |
Remove items with the
delete()method.
| |
Remove all items with the
clear()method.
| |
Web APIs are defined by W3C/WHATWG as standard interfaces built on top of the ECMAScript core language.
The URL API allows us to conveniently and accurately parse data from the URL
| |
With the Headers and Response APIs we can create HTTP responses with custom content, headers and status code.
| |
Browsers and JavaScript runtimes implement these APIs.
Deno is a modern (2018) open-source, standards-compliant runtime environment for executing JavaScript without a web browser.

| |
| |
Deno was created by Ryan Dahl as a response to his Node.js design regrets, .
If you have any questions, now is a good time to ask.
Thanks for listening
Dr Graeme Stuart