Skip to content

Instantly share code, notes, and snippets.

@chetanyakan
Last active July 9, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetanyakan/da4741619968ba88c689da98c67b285d to your computer and use it in GitHub Desktop.
Save chetanyakan/da4741619968ba88c689da98c67b285d to your computer and use it in GitHub Desktop.
JS Tidbits

JS Tidbits

  1. You can use bind to partially apply arguments to functions.
function map(cb, arr) {
    return arr.map(cb);
}
const superMap = map.bind(null, (item) => 'super ' + item);
superMap([1,2,3]);

// output: ["super 1", "super 2", "super 3"]
  1. You can use destructuring to find the length of an array
const {length} = [1,2,3];
console.log(length);

// output: 3
  1. Functions expressions and arrow functions aren't hoisted — only function declarations are.
add(1, 2);
// TypeError: add is not a function

// Function expression
var add = function(a, b) {
  return a + b;
}

// Arrow function
var add = (a, b) => a + b;

In the example above, the variable name add will be hoisted. But it will be assigned the value of undefined until a function is assigned to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment