Getting Started with ES6 and ES7 JavaScript

ES6 and ES7 Intro Image

Class 1

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

 

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

First Things First

Tell us about yourself.

  • Who are you?
  • What do you hope to get out of the class?
  • If you woke up tomorrow as an animal, what animal would you choose to be and why?

First Things First

Course Format

 

Need help?
Note the resources on the whiteboard

Thank you to our wonderful TAs!

Get Started: Tools

We'll be using the following tools in class:

 

  • Environment
  • Command Line Interface (your preference)
    • Terminal, cmd, X11, Git Bash, etc.
  • Text Editor (your preference)

Optional Activity

  1. Login to http://codepen.io (it's free!)
  2. For each CodePen exercise, click Edit on CodePen
  3. Click the Fork button at the top
  4. Edit away! Your changes will save to a new pen in your CodePen account

See the Pen Block-scoping: ES5 (Before) by Liz Shaw (@anythingcodes) on CodePen.

Background

What is ES6?

πŸ“„ Standard:

  • ECMAScript, abbreviated ES
  • Describes features, syntax, and behavior of all implementations
  • Think of it as a ruleset or blueprint


✍ Implementation:

  • JavaScript, the implementation of the ECMAScript standard
  • Implementations track standards
The standard, ECMAScript, is the ruleset, or blueprint
The implementation, JavaScript, is the language that uses that ruleset's plan, or built house/product

πŸ“ We'll implement the new features from versions 6 and 7 of ECMAScript using JavaScript

Why use ES6?

  • Sure, there are handy new methods, syntax, and easier-to-read code, but...
  • Top JavaScript libraries will be written in ES6. If you aren't familiar with ES6, you'll be at a disadvantage when trying to read documentation and build new features.
  • Backwards compatible
  • Integrates nicely with modern build tools


🌟 Above all, it's the new standard. This means that, in the near future, ES6 will be standard in each browser.

Today's Topics

  1. Transpiling
  2. Scopes & Variables
  3. String Helpers

Transpiling

Transpiling = Transformation + Compiling

 

Transpiling

Before: ES6

const getMessage = () => 'Hello World';

After: ES5

Waiting...

 

Benefits to Transpiling

  • Browser support
  • No need to wait for browsers/engines to catch up with the ECMAScript specification
  • Focus on writing clean, easy-to-read code — compile it to a cross-browser-compatible format
  • JavaScript will continue to evolve constantly; without transpilation, you'll miss out on innovations that make JavaScript more effective, efficient, and robust

Transpiling Setup

You can set this up on your machine using Node.js. For now, CodePen can use Babel to transpile ES6 for us.

Using the Babel transpiler in CodePen

Scopes & Variables

Scopes πŸ”¬

  1. Block Scopes (a.k.a. lexical scopes):
    {
      // anything within { } that isn't a function
    }
    
    if (isPhiladelphia) {
      var alwaysSunny = true;
    }
    • Examples: { }, if () { }, for() {}, while() { }, etc.
    • Purpose: Declare variables as close as possible, as local as possible, to where they will be used.
  2. Function Scopes:
    function func() {
      // function required
    }
    • Purpose: Code wrapped in a function effectively "hides" any enclosed variable or function declarations from the outside scope.

var only respects function scopes

Function vs. Block Scope »

Function Scope Problems

var variables are function-scoped.
They ignore block scoping.

See the Pen Block-scoping: ES5 (Before) by Liz Shaw (@anythingcodes) on CodePen.

View Solution

To fake block scoping, an immediately-invoked function expression (IIFE) is often used

The let Declaration

  • Block-scoped { }, unlike the function-scoped var
  • Value can be reassigned

Way easier than using an IIFE! πŸ†

Activity

Instructions can be found by editing the below pen on CodePen. Be sure to fork the pen first if you'd like to save your work to your account!

Activity 1

See the Pen Block-scoping by Liz Shaw (@anythingcodes) on CodePen.

View Solution

Activity 2

See the Pen Block-scoping with let (Before) by Liz Shaw (@anythingcodes) on CodePen.

View Solution

The const Declaration

  • Block-scoped, like let
  • Value cannot be reassigned
    • const stands for constant
  • Must be initialized on declaration
  • Communicates your variable's intent to other devs
  • Helps catch unintended changes
  • No more
    var PLZ_DONT_CHANGE = 42;
    πŸŽ‰

 

See the Pen const (Before) by Liz Shaw (@anythingcodes) on CodePen.

View Solution

Modifying Complex Values

Although you can't reassign a const's value, contents of complex values (objects & arrays) can be modified

const bindings and reassignments

const bindings and reassignments

const bindings and reassignments

const bindings and reassignments

const bindings and reassignments

Modifying Complex Values

Examples

See the Pen Updating the contents of const values by Liz Shaw (@anythingcodes) on CodePen.

Do you still need var?

No 😊

Only one potential case: If you have code that should be available from the global object (window in browsers), e.g. if you need to access code across frames or windows

πŸ‘† This is rare and should generally be avoided

Activity

Complete activities 1 and 2 in CodePen. Be sure to fork the pen first if you'd like to save your work to your account!

Activity 1

See the Pen const and let exercise by Liz Shaw (@anythingcodes) on CodePen.

View Solution

Activity 2

See the Pen const and let exercise #2 by Liz Shaw (@anythingcodes) on CodePen.

View Solution

String Helpers

String Helper Methods

Step aside, indexOf() — there are now easier ways to identify substrings:

  1. includes() returns true or false depending on if the given text is found anywhere within the string
  2. startsWith() returns true or false depending on if the given text is found at the beginning of the string
  3. endsWith() returns true or false depending on if the given text is found at the end of the string

String Helper Methods

Each method accepts two arguments:

  1. the text to search for
  2. an optional index from which to start the search
    • includes() and startsWith() include the character at that index, while endsWith() excludes the character at that index

Template Strings

Allow multi-line strings

Great for use with HTML templates

ES5:

'
' + '

Hello ' + str + '!

' + '
';

becomes

ES6:

`

Hello ${str}!

`;

Using Template Strings

const message = `User ${name} scored ${score} on the exam`;

 

  • Use backticks (` `) around the string
  • Use ${ } around any variable or expression
  • Work well with ternary operators, for example
    ${ userIsYoungerThan21 ? serveGrapeJuice() : serveWine() }

 

const message = `User ${name} ${score >= 60 ? 'passed' : 'failed'} the exam`;

Activity

Refactor this code to use template strings.

See the Pen Template Strings Activity by Liz Shaw (@anythingcodes) on CodePen.

View Solution

At-Home Challenge

Fruit Basket

var fruit = 'apple';

(function basket(){
    var fruit = 'banana';
    console.log(fruit);
})();

console.log(fruit);

var fruitBasket = {
    fruit: 'mango',
    getFruit: function(){
      console.log(fruit);
      fruit = 'orange';
    }
};
fruitBasket.getFruit();
console.log(fruit);

What would the following output to the console and why?

Next Week

  • Review at-home challenge from today's class
  • Arrow Functions
  • Arrow Functions and this
  • Array Helpers
  • Altering Arrays

 

Remember to do the at-home challenge!

Questions

?