5 - Intro to JavaScript.
Speck Academy 2021
How it works
Javascript basics
Functions
Collections
Objects
Promises
ES6+
Overview
1
2
3
4
5
6
7
1
How it works
How it works overview
About JavaScript
ECMAScript
JS engine
Execution context
Hoisting
About JavaScript
JavaScript is a cross-platform, object-oriented language based on prototypes used to make webpages interactive.
About JavaScript
JavaScript can change the way the webpage (DOM) looks. And, likewise, Node.js JavaScript on the server can respond to custom requests from code written in the browser.
Similar to Java, but in some ways different:
ECMAScript
ECMA - European Computer Manufacturers Association
Scripting language that forms the basis of JavaScript.
Standardized version of JavaScript - behaves the same way in all applications that support the standard.
JS engine
JS engine is a program, found in a hosting environment such as browser, that executes JS code. It consists of two main parts:
JS engine
Popular JS engines:
In order to improve performance, V8 translates the JS code directly into machine code during execution (JIT compiler).
V8 engine uses several threads for each process internally:
JS engine
Process
Thread
Execution context
Execution context is a wrapper in which the function is executed and which stores all of the relevant data of a function (variables, parameters, scope chain, value of this etc.).
The default execution context in JS is the Global Execution Context (GEC):
Each function gets a new execution context (internally implemented object) with a reference to it stored on top of the call stack.
Execution context
Call (execution) stack
global execution context
a = 0
‘#909dda’
pointer
Execution context
Call (execution) stack
global execution context
a = 0
first(a)
b = 1
‘#123aba’
pointer
Execution context
Call (execution) stack
global execution context
a = 0
first(a)
b = 1
second(b)
c = 2
‘#423fef’
pointer
Execution context
Call (execution) stack
global execution context
a = 0
first(a)
b = 1
second(b)
c = 2
third(c)
d = 3
‘#333eef’
pointer
Execution context
1. Creation phase (during compilation)
2. Execution phase
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Execution context - creation phase
Execution context object for thrones is created and reference to it is pushed to the Call stack.
Execution context object - creation phase
Execution context - execution phase
At execution phase values will be assigned to all of the declared variables in that execution context.
Execution context object - execution phase
Execution context
Variable object (VO) is a special object which stores variables, function declarations and parameters.
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Execution context
Scope is created inside functions (with ES6 scope also can be created inside if, for, do/while blocks).
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Execution context
Scope chain
Global scope
[VOglobal]
one() scope
[VOone] + [VOglobal]
two() scope
[VOtwo] + [VOone] + [VOglobal]
scope chain
Execution context
“this” variable - The last step in the creation of the execution context object is the assignment of value to the “this” property.
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Execution context
Rules for “this” keyword
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Execution context
“this” keyword in ES6 arrow functions
EXECUTION CONTEXT OBJECT
Variable object (VO)
Scope chain
“this” variable
Hoisting
Variable hoisting - var
Variable hoisting - let and const
Hoisting
Function hoisting
This is great video that explains how JavaScript works!
2
JavaScript basics
Javascript basics overview
Grammar and types
Control flow and errors
Loops
Grammar and types
JS is a case-sensitive and uses Unicode character set.
Comments:
Variable declarations:
Grammar and types
Latest ECMAScript standard defines eight data types:
Control flow
Block statements are commonly used with control flow statements (if, for, while)
Conditional statements:
Errors
You can throw exceptions using the throw statement and handle them using the try...catch statements.
try...catch statement:
Find out more about control flow and error handling.
Loops
Loops offer a quick and easy way to do something repeatedly.
Statements for loops (find more here):
3
Functions
Functions
Defining functions
Closures
Callback functions
Arrow functions
Defining functions
A function is a JavaScript procedure - a set of statements that performs a task or calculates a value.
A function definition (function declaration, function statement) consists of the function keyword, followed by:
// Function declaration
function getFullName(first, last = ‘Moe’){� var fullName = first + ‘ ’ + last;
return fullName;
}
// Function call
getFullName(‘John’, ‘Doe’);
// Console
> John Doe
Defining functions
Functions can also be created like a function expression.
Function expression can be named function (for recursive purpose) or anonymous which means it doesn’t have to have a name.
Function expression can’t be hoisted, so if you call function before it’s definition, error will be thrown.
// Function expression
var getFullName = function(first, last){� return first + ‘ ’ + last;
}
// Function call
getFullName(‘John’, ‘Doe’);
// Console
> John Doe
Closures
A closure is an expression (most commonly function) that can have free variables together with an environment that binds those variables (that “closes” the expression).
Nested Function is a closure, this means that a nested function can “inherit” the arguments and variables of its containing function. In other words, the inner function contains the scope of the outer function.
Closures
Closures provide us a sort of encapsulation for the variables of the inner function.
Nested Function is a closure, this means that a nested function can “inherit” the arguments and variables of its containing function. In other words, the inner function contains the scope of the outer function.
// Closure
function outside(x){
var outLength = x;
function inside(y){
var innLength = y;� return outLength - innLength;
}
� return inside;
}
// Function call
outside(100)(70);
// Console
> 30
Callback functions
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
Callback function is often used to continue code execution after asynchronous operation has completed.
Example on the right simulates a request to server. Try out here!
// Function that simulates a server request
function serverRequest(query, callback){
setTimeout(function(){
var response = query + ‘full!’;� callback(response);
}, 5000);
}
// Function which will be passed as an callback
function getResults(results){
console.log(‘Server response: ’ + results);
}
// Function call
serverRequest(‘The glass is half ’, getResults);
// Console response after 5 seconds
> Server response: The glass is half full!
Arrow functions
An arrow function expression has a shorter syntax compared to function expressions and does not have its own this, arguments, super, or new.target.
Two factors influenced the introduction of arrow functions:
Read more about arrow functions
// Same thing can be written in two ways�// Function expression
const f1 = function(x){ return x; }
// Arrow function expression
const f2 = x => x;
4
Collections
Collections overview
Indexed collections - Arrays
Keyed collections
Indexed collections - Arrays
Arrays are collections of data which are ordered by an index value.
JavaScript doesn’t have an explicit array data type. However, you can use the predefined Array object and its methods to work with arrays in your applications.
Try to become familiar with an array methods, some of the most common methods are listed on the next slide.
All of the important things about arrays can be found here.
Indexed collections - Arrays
Array methods are often used in development, and some the most common are:
Keyed collections
Collections of data which are ordered by a key.
ECMAScript 2015 introduces a new data structure to map values to values.
Main difference between Map and Set:
Read more about keyed collections
Keyed collections
Object and Map compared:
Use maps over objects:
5
Hands-on
Hands-on
6
Homework
Homework
Link to Homework