{%hackmd BJrTq20hE %} # ES6 CheatSheet - <code>let</code> allow javascript to return error when variable was called twice, u can rewrite variable through <code>variable = "String"</code>. - <code>"use strict"</code> is a good debugging tool - using <code>let</code> in function argument for same variable wont overwrite the value of the variable Example: ```javascript= function checkScope() { "use strict"; let i = "function scope"; if (true) { let i = "block scope"; console.log("Block scope i is: ", i); } console.log("Function scope i is: ", i); return i; } // return = // Block scope i is: block scope // Function scope i is: function scope ``` - <code>const</code> is read-only, value cant change, it is a good practice to write const variable in **CAPS**. - <code>const variable = ["array"]</code> the value of array can be change if we change 1 by 1 using array index. Example: ```javascript= const s = [5, 7, 2]; function editInPlace() { "use strict"; //s = [2, 5, 7]; s[0] = 2; s[1] = 5; s[2] = 7; } editInPlace(); ``` - <code>Object.freeze(CONSTANT)</code> allow error to be return on const object.property when object.property was changed - u can return a function inside a function to pass it to other variable to call the return function, [**More Explaination**](https://stackoverflow.com/questions/7629891/functions-that-return-a-function?fbclid=IwAR3ZgGI71adRjmYakhhuilvas_RD2N7dl6FsTchMas2fE7dUXqwwl2T2mOg), this is called [**closure**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) ```javascript= let counter = () => { let count = 0; return () => alert(count++); } const x = counter(); x(); x(); x(); ``` - ES6 allow u to set a default parameter on return function - REST function - <code>...</code> can let indefinite parameter to be define as an array ```javascript= const sum = (function() { return function sum(...args) { // reduce() executes a function on element in an array,result to single output // parameter.reduce((array, var) => argument, var-value) return args.reduce((a, b) => a + b, 0); }; })(); // () to change function to value to call in future console.log(sum(1, 2, 3, 4)); ``` ### Higher Order Function ### Higher Order Arrow Function Example: ```javascript= const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]; const squareList = (arr) => { // filter an array with an argument // array.filter(variable => argument) const squaredIntegers = arr.filter(num => Number.isInteger(num) && num > 0).map(x => x * x); return squaredIntegers; } const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers); ```