javaScript ES6

Hello, my Name is Christopher. I'm learning Frontend Web Devlopment on my own. I've found several resourses available to anyone wanting to learn web development.

I use a few different web sites for free courses and reference material, like "Free code Camp", "W3 Schools" and "MDN Web Docs". I use these sites for theory, testing and documentation. I also found lots of videos on YouTube that were great for beginers right up to more advanced programming. I did find the theory and having lectures from videos beneficial but they just didn't complete the learning process, I had to take notes too. I try to reference the sites I used for source material when possible in my notes.

I've started to build several reference document web pages to practice building web pages while taking notes on different subjects.


Welcome to ES6

ECMAScript, or ES, is a standardized version of JavaScript. Because all major browsers follow this specification, the terms ECMAScript and JavaScript are interchangeable.

Most of the JavaScript you've learned up to this point was in ES5 (ECMAScript 5), which was finalized in 2009. While you can still write programs in ES5, JavaScript is constantly evolving, and new features are released every year.

ES6, released in 2015, added many powerful new features to the language. In this course, you'll learn these new features, including arrow functions, destructuring, classes, promises, and modules.




ES6 Variables

Free Code Camp

Softcorp TV

var vs const vs let


Compare Scopes of the var and let Keywords - FCC

let

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it.

Global Scope

At the top level of programs and functions, let, unlike var, does not create a property on the global object. For example:

            
  var x = 'global';
  let y = 'global';

  console.log(this.x); // "global"
  console.log(this.y); // undefined
            
          

If you are unfamiliar with let, check out this challenge.

When you declare a variable with the var keyword, it is declared globally, or locally if declared inside a function.

The let keyword behaves similarly, but with some extra features. When you declare a variable with the let keyword inside a block, statement, or expression, its scope is limited to that block, statement, or expression.

Example:

          
  var numArray = [];
  for (var i = 0; i < 3; i++) {
    numArray.push(i);
  }
  console.log(numArray);
  console.log(i);
          
        

Here the console will display the values [0, 1, 2] and 3.

With the var keyword, i is declared globally. So when i++ is executed, it updates the global variable. This code is similar to the following:

          
  var numArray = [];
  var i;
  for (i = 0; i < 3; i++) {
    numArray.push(i);
  }
  console.log(numArray);
  console.log(i);
          
        

Here the console will display the values [0, 1, 2] and 3.

This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the i variable. This is because the stored function will always refer to the value of the updated global i variable.

          
  var printNumTwo;
  for (var i = 0; i < 3; i++) {
    if (i === 2) {
      printNumTwo = function() {
        return i;
      };
    }
  }
console.log(printNumTwo());
          
        

Here the console will display the value 3.

As you can see, printNumTwo() prints 3 and not 2. This is because the value assigned to i was updated and the printNumTwo() returns the global i and not the value i had when the function was created in the for loop. The let keyword does not follow this behavior:

          
  let printNumTwo;
  for (let i = 0; i < 3; i++) {
    if (i === 2) {
      printNumTwo = function() {
        return i;
      };
    }
  }
  console.log(printNumTwo());
  console.log(i);
          
        

Here the console will display the value 2, and an error that i is not defined.

i is not defined because it was not declared in the global scope. It is only declared within the for loop statement. printNumTwo() returned the correct value because three different i variables with unique values (0, 1, and 2) were created by the let keyword within the loop statement.


Mutate an Array Declared with const - FCC

const

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration). However, if a constant is an object or array its properties or items can be updated or removed.

The const declaration has many use cases in modern JavaScript.

Some developers prefer to assign all their variables using const by default, unless they know they will need to reassign the value. Only in that case, they use let.

However, it is important to understand that objects (including arrays and functions) assigned to a variable using const are still mutable. Using the const declaration only prevents reassignment of the variable identifier.

            
  const s = [5, 6, 7];
  s = [1, 2, 3];
  s[2] = 45;
  console.log(s);
            
        

s = [1, 2, 3] will result in an error.

The console.log will display the value [5, 6, 45].

As you can see, you can mutate the object [5, 6, 7] itself and the variable s will still point to the altered array [5, 6, 45]. Like all arrays, the array elements in s are mutable, but because const was used, you cannot use the variable identifier s to point to a different array using the assignment operator.


Prevent Object Mutation

Object.freeze()

The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

As seen in the previous challenge, const declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function Object.freeze to prevent data mutation.

Any attempt at changing the object will be rejected, with an error thrown if the script is running in strict mode.

            
  let obj = {
    name:"FreeCodeCamp",
    review:"Awesome"
    };

  Object.freeze(obj);
  obj.review = "bad";
  obj.newProp = "Test";

  console.log(obj); 
            
        

The obj.review and obj.newProp assignments will result in errors, because our editor runs in strict mode by default, and the console will display the value { name: "FreeCodeCamp", review: "Awesome" }.



Arrow Functions

Free Code Camp

Softcorp TV

Arrow Functions

Arrow functions

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.

Differences & Limitations:

  • Does not have its own bindings to this or super, and should not be used as methods.
  • Does not have new.target keyword.
  • Not suitable for call, apply and bind methods, which generally rely on establishing a scope.
  • Can not be used as constructors.
  • Can not use yield, within its body.

Basic Syntax

One param. With simple expression return is not needed:

            
    param => expression
            
          

Multiple params require parentheses. With simple expression return is not needed:

            
    (param1, paramN) => expression
            
          

Multiline statements require body braces and return:

            
    param => {
      let a = 1;
      return a + param;
    }
            
          

Multiple params require parentheses. Multiline statements require body braces and return:

            
    (param1, paramN) => {
      let a = 1;
      return a + param1 + paramN;
    }
            
          

Advanced Syntax

To return an object literal expression requires parentheses around expression:

            
    params => ({foo: "a"}) // returning the object {foo: "a"}
            
          

Rest parameters are supported:

            
    (a, b, ...r) => expression
            
          

Default parameters are supported:

            
    (a=400, b=20, c) => expression
            
          

Destructuring within params supported:

            
    ([a, b] = [10, 20]) => a + b;  // result is 30
    ({ a, b } = { a: 10, b: 20 }) => a + b; // result is 30
            
          

Use Arrow Functions to Write Concise Anonymous Functions

In JavaScript, we often don't need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don't need to name these functions because we do not reuse them anywhere else.

To achieve this, we often use the following syntax:

          
  const myFunc = function() {
    const myVar = "value";
    return myVar;
  }
          
        

ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax:

          
  const myFunc = () => {
    const myVar = "value";
    return myVar;
  }
          
        

When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword return as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:

          
  const myFunc = () => "value";
          
        

This code will still return the string value by default.




Classes

Free Code Camp

Softcorp TV

Classes



Symbols

Free Code Camp

Softcorp TV

Symbols



Template Literals

Free Code Camp

Softcorp TV

Template Literals



...spread operator and rest operator

Free Code Camp

Softcorp TV

...spread operator and rest operator



Destructuring

Free Code Camp

Softcorp TV

Destructuring



Import Export Modules

Free Code Camp

Softcorp TV

import/export



Proxies

Free Code Camp

Softcorp TV

Proxies



Map data structure & Map Object

Free Code Camp

Softcorp TV

Map Object