Let's Talk about
Javascript
Johan Esteban Higuita
Software Developer at Talent.com
Bioengineer - Universidad de Antioquia
Session 2
Let's Talk about Javascript
Let's Talk about Javascript
Let's Talk about Javascript
5
4
3
2
1
Let's Talk about Javascript
ES5 (and older)
Supported in basically all browser, including old IE
Only var, not let or const
ES6 (and newer): Modern JavaScript
Supported in modern browsers, can (mostly) be transpiled
Many new features that help us write cleaner, better & faster code
JavaScript Evolution
let - const
Arrow functions
Destructuring
Template strings
Classes
Enhanced object literals
Modules
...
async / await
Let's Talk about Javascript
ES5
ES6
Javascript is a "forgiving" language
Let's Talk about Javascript
Browser
Web Workers
Node.js
Window
WorkerGlobalScope
global
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
function fn() {
var textVar = "with var";
let textLet = "with let";
const textConst = "with const";
console.log(textVar); // output: with var
console.log(textLet); // output: with let
console.log(textConst); // output: with const
}
console.log(textVar); // Reference Error
console.log(textLet); // Reference Error
console.log(textConst); // Reference Error
Function Scope
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
if(condition) {
var textVar = "with var";
let textLet = "with let";
const textConst = "with const";
console.log(textVar); // output: with var
console.log(textLet); // output: with let
console.log(textConst); // output: with const
}
console.log(textVar); // output: with var
console.log(textLet); // Reference Error
console.log(textConst); // Reference Error
Block Scope
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
var x; // Declaration
x = "Hello World"; // Assigment
// All in one
var y = "Hello World";
Declaration: Variable is registered using a given name
Assigment A specific value is assigned to the variable
Can be reassigned?
Let's Talk about Javascript
var textVar = "with var";
let textLet = "with let";
const textConst = "with const";
//Reassignment
textVar = "new value";
textLet = "new value";
textConst = "new value"; // TypeError: Assignment to constant variable.
Can be reassigned?
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
var textVar = "with var";
let textLet = "with let";
const textConst = "with const";
//Redeclaration
var textVar = "new value";
let textLet = "new value"; //SyntaxError: Identifier 'textLet' has already been declared
const textConst = "new value"; //SyntaxError: Identifier 'textConst' has already been declared
(in the same scope)
Can be redeclared?
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
function hello(name){
console.log(`Hello ${name}!!`);
}
hello("Peter"); //Hello Peter!!
Variables and functions declarations are physically moved to the top of the current scope.
hello("Peter"); //Hello Peter!!
function hello(name){
console.log(`Hello ${name}!!`);
}
✓
✓
Let's Talk about Javascript
var name = "Peter";
var age = 24;
With Variables
var name;
var age;
name = "Peter";
age = 24;
Hoisting
Let's Talk about Javascript
console.log(textVar); // undefined
console.log(textLet); // ReferenceError: textLet is not defined
console.log(textConst); // ReferenceError: textConst is not defined
var textVar = "with var";
let textLet = "with let";
const textConst = "with const";
Let's Talk about Javascript
var | let | const | |
---|---|---|---|
Stored in Global scope | ✓ | ✕ | ✕ |
Function Scope | ✓ | ✓ | ✓ |
Block Scope | ✕ | ✓ | ✓ |
Can Be Reassigned? | ✓ | ✓ | ✕ |
Can Be Redeclared? | ✓ | ✕ | ✕ |
Can Be Hoisted? | ✓ | ✕ | ✕ |
Let's Talk about Javascript
Variable shadowing occurs when a variable is declared in certain scope has the same name defined on other scope
These variables can co-exist with the same name
const name = "neuvoo";
if(true){
const name = "talent";
console.log(name); // talent
}
console.log(name); // neuvoo
Let's Talk about Javascript
Converting mistakes into errors
'use strict';
num = 10; //ReferenceError
'use strict';
var undefined = 15; // TypeError
var Infinity = 25; // TypeError
'use strict';
var o = { p: 1, p: 2 }; // Syntax error
function sum(a, a, c) { // !!! syntax error
'use strict';
return a + a + c; // wrong if this code ran
}
'use strict';
var sum = 015 + 110; //Syntax error
Makes it impossible to accidentally create global variables.
use of reserved words in variable declaration
Requires that all properties named in an object literal be unique.
A leading zero prefix a syntax error.
Requires that function parameter names be unique.
Let's Talk about Javascript
Let's Talk about Javascript
Thank you!