Discussion, Questions, and Resources for Part 1 (JavaScript - April 2018 Cohort)

Strict Mode

I wasn’t familiar with "use strict" (i.e. Strict mode) so I looked it up.

In ES6: Explore Differences Between the var and let Keywords, it states:

Note the “use strict”. This enables Strict Mode, which catches common coding mistakes and “unsafe” actions.

That’s helpful, but is it necessary to use it? What are the suggested best practices?

According to MDN’s Strict mode page:

ECMAScript 5’s strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of “sloppy mode”. Strict mode isn’t just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don’t rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.

Here are some other resources about Strict mode that might be helpful:

In general, it seems to be best practice to use Strict mode on new projects but not necessarily to add it to existing projects.

map(), filter(), and reduce()

So, unfortunately, freeCodeCamp Beta doesn’t do a great job of introducing the map, filter, and reduce functions before they’re needed. Here’s a quick summary:

  • map()
    MDN map() page | fCC Guide on map()

    The map() method creates a new array with the results of calling a provided function on every element in the calling array.

    Example:

    var numbers = [1, 4, 9];
    var roots = numbers.map(Math.sqrt);
    // roots is now [1, 2, 3]
    // numbers is still [1, 4, 9]
    
  • filter()
    MDN filter() page | fCC Guide on filter()

    The filter() method creates a new array with all elements that pass the test implemented by the provided function.

    Example:

    const words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];
    
    let longWords = words.filter(word => word.length > 6);
    
    // Filtered array longWords is ["exuberant", "destruction", "present"]
    
  • reduce()
    MDN reduce() page | fCC Guide on reduce()

    The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

    Example:

    var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
       return accumulator + currentValue;
    }, 0);
    // sum is 6
    

:sunny:

2 Likes