Tips and tricks to make the most of JavaScript and avoid gotchas.

Joined May 2016
Photos and videos
Arrow functions are not yet supported in Safari, IE and Opera Mini. #javascript #es6 #tip caniuse.com/#feat=arrow-func…

1
5
// ES5 var posNums = nums.filter(function (x) { return x > 0; }); // #ES6 var posNums = nums.filter(x => x > 0); #javascript #tip
2
3
#ES6 arrow functions do not have their own 'this'. The value of this inside an arrow function is always inherited from the enclosing scope.
2
5
JavaScript Tips retweeted
Code is temporary. It exists while it is useful. If code is replaced by better code, good! If code can be deleted, celebrate! #JavaScript
3
94
173
Shorthand for #es6 arrow function with single argument: var f = x => { return x*x; }; // equivalent to var f = (x) => { return x*x; };
1
2
1
Shorthand for #es6 arrow function with single return statement: var f = (x) => x*x; // equivalent to var f = (x) => { return x*x; };
1
ES6 arrow functions: var normalFunc = function(x) { return x * x; }; var arrowFunc = (x) => { return x*x; }; #javascript #tips
5
5
#javascript 'new' keyword: function A() { this.x = 1; this.y = 2; } var a1 = new A(); var a2 = new A(); a1.x ; a1.x; // 2 a2.x; // 1
3
5
Usage of the 'arguments' local variable: function foo() { console.log(arguments); } foo(1, 2, 3, 4); // [1, 2, 3, 4] #javascript #tip
1
Convert CSV to array: var str = "apple, peach, orange"; var fruitsArray = str.split(","); // ['apple', 'peach', 'orange'] #javascript #tip
3
#JavaScript array to csv: var fruits = ['apple', 'peaches', 'oranges']; var str = fruits.valueOf(); // apple,peaches,oranges #js #tips
3
Remove duplicates from array: var deduped = [ 1, 1, 'a', 'a'].filter(function (x, i, arr) { return arr.indexOf(x) === i; }); // [1, 'a']
1
1
Flattening an array : var myArray = [[1, 2], [], [3]]; var flatArray = [].concat.apply([], myArray); // flatArray is [1, 2, 3] #javascript
1
2
Getting a UNIX timestamp in #JavaScript: var dateTime = Date.now(); var timestamp = Math.floor(dateTime / 1000); #tips #js

1
To see a function's code, concatenate it with the empty string : console.log(funcVariable ''); #javascript #tips
The && operator first evaluates the expression on the left. If falsy, false is returned; it does not bother to evaluate the right operand.
1
JavaScript Falsy Values: null, false, 0 undefined, NaN, and “” (this last item is an empty string). #javascript #tips
Infinity, which is a special number like NaN, is truthy; it is not falsy, while NaN is falsy. #javascript #tips
Calculate max/min in an array: var arr = [1, 2, 3, 4]; Math.max.apply(null, arr) // 4 Math.min.apply(null, arr) // 1 #javascript #tips
1