Search…
JS
As every web app, ours rely on a bunch of JS lines of code
We favor ECMAScript 6 (ES6) syntax.

ES6 Useful Features

Default parameters

In ES6, we can put the default values right in the signature of the functions.
1
var calculateArea = function(height = 50, width = 80) {
2
// write logic
3
...
4
}
Copied!

Template Literals

We can use a new syntax ${PARAMETER} inside of the back-ticked string.
1
var name = `Your name is ${firstName} ${lastName}.`
Copied!

Multi-line Strings in ES6

Just use back-ticks.
1
let lorem = `Lorem ipsum dolor sit amet,
2
consectetur adipiscing elit.
3
Nulla pellentesque feugiat nisl at lacinia.`
Copied!

Arrow Functions

Arrow functions – also called fat arrow functions, are a more concise syntax for writing function expressions. They utilize a new token, =>, that looks like a fat arrow. Arrow functions are anonymous and change the way this binds in functions.
There are a variety of syntaxes available in arrow functions, of which MDN has a thorough list.
Basic Syntax with Multiple Parameters (from MDN)
1
// (param1, param2, paramN) => expression
2
3
// ES5
4
var multiplyES5 = function(x, y) {
5
return x * y;
6
};
7
8
// ES6
9
const multiplyES6 = (x, y) => { return x * y };
Copied!
Curly brackets and return aren’t required if only one expression is present. The preceding example could also be written as:
1
const multiplyES6 = (x, y) => x * y;
Copied!
Parentheses are optional when only one parameter is present but are required when no parameters are present.
1
//ES5
2
var phraseSplitterEs5 = function phraseSplitter(phrase) {
3
return phrase.split(' ');
4
};
5
6
//ES6
7
const phraseSplitterEs6 = phrase => phrase.split(" ");
Copied!
1
//ES5
2
var docLogEs5 = function docLog() {
3
console.log(document);
4
};
5
6
//ES6
7
var docLogEs6 = () => { console.log(document); };
Copied!
Last modified 2yr ago
Copy link