Skip to content
This repository has been archived by the owner on Mar 7, 2019. It is now read-only.

ES6 Basics

Bill Wong edited this page Mar 29, 2017 · 2 revisions

There are three main syntax updates used in ES6 that we'll want you to use:

Variable Types

Basically, never use var. If you have a variable that needs to be modified later, use let. If it doesn't need to be modified, use const.

//es6:
const str = "poop";
let updatableStr = "p";

//es5
var str = "poop";
var updatableStr = "p";

Arrow Functions

The syntax of anonymous functions looks a lot better now:

//es6
setTimeout(() => {
  alert('hello');
}, 1000);

//es5
setTimeout(function() {
  alert('hello');
}, 1000);

Frontend Only: Importing/Exporting

In any frontend code, use the import syntax instead of require, and export default instead of module.exports. For backend code such as models, continue using the es5 syntax here because this isn't supported in Node.

//es6
import mongoose from 'mongoose';

//es5
var mongoose = require('mongoose');
//es6
export default routes;

//es5
module.exports = routes;