JS Es15
JS Es15
ES2015 or ES6?
What should we call it?
● You will see both ES2015 and ES6 used to describe the current widely-released
version of JavaScript
● The official specification is called ECMAScript 2015 Language, but that is for
the language specification and NOT the implemented coding language
● There is not an officially preferred name; you may use either ES2015 or ES6 to
refer to the current JS release
ES2015 JavaScript
Template Literals
Life Before Template Literals
function sayHello(name) { ● Traditionally, when you want to use a
console.log('Hello ' + name + '!'); variable's value as part of a string, you have
} to use string concatenation to combine the
pieces of the string together
sayHello('John'); ● You have to be careful with spacing and
punctuation, otherwise you might get
something like: HelloJohn!
Template Literals to the Rescue
function sayHello(name) { ● A template literal is a special type of string
console.log(`Hello ${name}!`); that is denoted by backticks (`) instead of
} single or double quotes
● Inside a template literal, everything is
sayHello('John'); assumed to be a character unless you
interpolate it
● Interpolation is the process of evaluating a
JavaScript expression or variable and
outputting its value
● To interpolate, surround the variable or
expression with the interpolation syntax:
${someVar}
● When using template literals, our statements flow more naturally and we aren't
prone to spacing or punctuation errors
ES2015 JavaScript
Default Parameter Values
Life Before Default Parameter Values
function greet(name) { ● If an argument is not passed into a function
alert(`Hello ${name}!`); when it is called, the parameter will be
} undefined
greet('Jane');
Shorthand: Concise Body
// shorthand ● If an arrow function simply needs to return
let add = (a, b) => a + b; a value that can be easily accessed or
computed, you can use the concise body
// equivalent to: shorthand
let add = (a, b) => { ● With a concise body, you DO NOT use curly
return a + b; braces to specify the body of the arrow
}; function
● You simply place a variable or expression
let sum = add(5,4); right after the arrow, and that value will be
console.log(sum); returned by the arrow function
Shorthand: Concise Body
let getStats = (a, b) => ({ ● When using the concise body shorthand,
sum: a + b, you must surround a returned object literal
difference : a - b, with parenthesis
product: a * b,
quotient: a / b
});
// Most verbose
let getFullName = (person) => {
return `${person.firstName} ${person.lastName}`;
};
// same as:
add.apply(null, enteredValues );
// same as:
add(5, 2, 7);
Spreading Values in an Array
let low = [0, 1, 2]; ● You can use the spread operator to unfurl
let high = [7, 8, 9]; the values of an array into another array
console.log(n);
// [0,1,2,3,4,5,6,7,8,9]
Spreading a String
let str = 'Code'; ● You can use the spread operator to spread
the characters of a string into an array
let chars = [...str];
console.log(chars);
// [ 'C', 'o', 'd', 'e' ]
ES2015 JavaScript
Object Literal Property Shorthand
Property Names in Object Literals
function getStats(a, b) { ● This function has local variables
let resultAdd = a + b; resultAdd and resultSub, and it
let resultSub = a - b; packages those values up in an object literal
before returning it
return { ● We picked the names of the local variables,
sum: resultAdd, as well as the names of the properties in the
difference : resultSub object (sum, difference )
}; ● When we log stats to the console, we will
} see an object with properties sum and
difference
let stats = getStats(4,2);
console.log(stats);
Property Names in Object Literals
function getStats(a, b) { ● There is nothing that says our property
let sum = a + b; names cannot be the same as the variable
let difference = a - b; names we are accessing
● This example functions the exact same as
return { before
sum: sum, ● When you are defining an object literal, the
difference : difference word to the left of the : is the name of the
}; property you are choosing to put on the
} object
● The value after the : is going to be the
let stats = getStats(4,2);
value for that property
console.log(stats);
● So in this example, starting on the right, we are accessing the sum variable,
getting its value, and setting that as the value of the sum property for the object
literal we are creating
Property Name Shorthand
function getStats(a, b) { ● When a property name is the same as the
let sum = a + b; variable that will fill its value, you can
let difference = a - b; simply use the property/variable name
once and omit the :
return { ● This shorthand is often written on one line
sum,
difference
};
// or:
return { sum, difference };
}
console.log(firstName); // 'Jane'
console.log(lastName); // 'Doe'
console.log(age); // 29
Object Destructuring: Default Values
let p1 = { ● You can specify a default value for a
firstName: 'Jane', destructured property
lastName: 'Doe', ● The default value will be used if the object
age: 29 does not contain the property you are
}; trying to unpack
console.log(firstName); // 'Jane'
console.log(gender); // 'F'
Object Destructuring: Renaming Properties
let p1 = { ● This is a non-obvious syntax
firstName: 'Jane', ● First you specify the property you want to
lastName: 'Doe', unpack from the object
age: 29 ● You then specify a new name for it after a :
}; ● The variable that is created with the
unpacked value will use the new name
let { firstName: fName } = p1;
console.log(fName); // 'Jane'
Array Destructuring in Function Parameters
function myFunc([op1, op2]) {
console.log(op1);
console.log(op2);
}
myFunc(myArray);
Object Destructuring in Function Parameters
function myOtherFunc ({ lat, lng }) {
console.log(lat);
console.log(lng);
}
let address = {
street: '123 Main St' ,
city: 'Birmingham' ,
state: 'AL',
lat: '33.514961' ,
lng: '86.807853'
};
myOtherFunc (address);
ES2015 JavaScript
JavaScript Modules
JavaScript Modules
● JavaScript module syntax allows code to be exported from and imported into
other files
● Relies on the import, export, and default keywords
Exporting at Declaration
// myStuff.js ● Exporting is a passive act; you are simply
marking something as being available for
export function sayHello(name) { import elsewhere
alert(`Hello ${name}!`); ● To make a function or variable available for
} import in another module (file), simply
place the export keyword in front of the
export let cityCode = 'BHM'; declaration
// Good
let cityCode = 'BHM';
export default cityCode;
// app.js
import { sayHello } from './myStuff.js' ;
sayHello('Jane');
Importing All Named Exports
// myStuff.js ● You can also use the wildcard syntax to
export function sayHello(name) { grab all named exports from a module
alert(`Hello ${name}!`); ● Use the as keyword to specify a name (that
} you came up with) in which to store the
imported code
export let cityCode = 'BHM'; ● In this case, helpers will be an object with
properties sayHello, cityCode, and
export const MAX_SEATS = 20; MAX_SEATS
// app.js
import * as helpers from './myStuff.js' ;
helpers.sayHello('Jane');
Import a Default Export
// myStuff.js ● To import a default export, you simply
export default function sayHello(name) { specify a name in which to store that
alert(`Hello ${name}!`); default export, without the use of curly
} braces
○ import { greet } means "import the
export let cityCode = 'BHM'; named export greet into this module"
○ import greet means "import the default
export const MAX_SEATS = 20; export, whatever it is called, into this
module and refer to it as greet
● To clarify, the name in the import
// app.js statement is something we came up with; it
import greet from './myStuff.js' ; doesn't have to match the name of the
default export (although it can if you'd like)
greet('Jane');
Browser Support
● At this moment in time, most browsers do not support module import syntax
● To be able to use import/export, you need to use a packager
○ There are many packagers available, and many development frameworks include packager
scripts for you