Skip to content

Instantly share code, notes, and snippets.

@rogerwschmidt
Forked from bwreid/01 ES6.md
Last active September 22, 2022 11:52
Show Gist options
  • Save rogerwschmidt/194b4a026af06ce8282aaa22ef0e007f to your computer and use it in GitHub Desktop.
Save rogerwschmidt/194b4a026af06ce8282aaa22ef0e007f to your computer and use it in GitHub Desktop.

ECMAScript 2015 / ES6

Objectives

  • Concatenate strings and variables with template strings
  • Declare variables with only let and const
  • Write anonymous functions with arrow functions
  • Declare defaults to functions parameters

Resource

https://github.com/rogerwschmidt/es6-lesson-workspace

Practice

  • Update the following code to use ES6 features:

    var winners = [ 'Otis Duncan', 'Melba Frank', 'Damon Adams' ]
    
    function printWinnersByPlace (people) {
      var first = winners[0]
      var rest = winners.slice(1)
      return 'In 1st place is ' + first + '! Followed up by ' + rest.join(' and ') + '.'
    }
    
    printWinnersByPlace(winners)
  • Update the following code to be terser and use ES6 features:

    function orderBy (array, key, options) {
      options = options || {}
      
      // create a copy of the array to sort
      var toSort = array.slice()
    
      if (options.asc) {
        toSort.sort(function (a, b) {
          return a[key] > b[key] ? 1 : -1
        })
      }
    
      if (options.desc) {
        toSort.sort(function (a, b) {
          return a[key] < b[key] ? 1 : -1
        })
      }
    
      return toSort
    }
    
    const people = [
      { firstName: 'Dustin', lastName: 'French', age: 23 },
      { firstName: 'Norman', lastName: 'Sanders', age: 44 },
      { firstName: 'Jessica', lastName: 'Glover', age: 39 },
      { firstName: 'Alfred', lastName: 'Hicks', age: 31 }
    ]
    
    orderBy(people, 'lastName', { desc: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment