JavaScript Tips
@javascript_tips
Tips and tricks to make the most of JavaScript and avoid gotchas.
Вам может понравиться
Arrow functions are not yet supported in Safari, IE and Opera Mini. #javascript #es6 #tip caniuse.com/#feat=arrow-fu…
// ES5 var posNums = nums.filter(function (x) { return x > 0; }); // #ES6 var posNums = nums.filter(x => x > 0); #javascript #tip
#ES6 arrow functions do not have their own 'this'. The value of this inside an arrow function is always inherited from the enclosing scope.
Code is temporary. It exists while it is useful. If code is replaced by better code, good! If code can be deleted, celebrate! #JavaScript
Shorthand for #es6 arrow function with single argument: var f = x => { return x*x; }; // equivalent to var f = (x) => { return x*x; };
Shorthand for #es6 arrow function with single return statement: var f = (x) => x*x; // equivalent to var f = (x) => { return x*x; };
ES6 arrow functions: var normalFunc = function(x) { return x * x; }; var arrowFunc = (x) => { return x*x; }; #javascript #tips
#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
Usage of the 'arguments' local variable: function foo() { console.log(arguments); } foo(1, 2, 3, 4); // [1, 2, 3, 4] #javascript #tip
Use #es6 in your javascript code today with @babeljs! hacks.mozilla.org/2015/06/es6-in…
hacks.mozilla.org
ES6 In Depth: Using ES6 today with Babel and Broccoli – Mozilla Hacks - the Web developer blog
ES6 In Depth is a series on new features being added to the JavaScript programming language in the 6th Edition of the ECMAScript standard, ES6 for short. ES6 ...
Convert CSV to array: var str = "apple, peach, orange"; var fruitsArray = str.split(","); // ['apple', 'peach', 'orange'] #javascript #tip
#JavaScript array to csv: var fruits = ['apple', 'peaches', 'oranges']; var str = fruits.valueOf(); // apple,peaches,oranges #js #tips
Remove duplicates from array: var deduped = [ 1, 1, 'a', 'a'].filter(function (x, i, arr) { return arr.indexOf(x) === i; }); // [1, 'a']
Flattening an array : var myArray = [[1, 2], [], [3]]; var flatArray = [].concat.apply([], myArray); // flatArray is [1, 2, 3] #javascript
Getting a UNIX timestamp in #JavaScript: var dateTime = Date.now(); var timestamp = Math.floor(dateTime / 1000); #tips #js
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.
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
United States Тренды
- 1. Dodgers 775K posts
- 2. World Series 412K posts
- 3. Blue Jays 125K posts
- 4. Yamamoto 242K posts
- 5. Will Smith 54.1K posts
- 6. jungkook 358K posts
- 7. Nigeria 794K posts
- 8. Miguel Rojas 46.8K posts
- 9. #River 5,132 posts
- 10. Yankees 16.7K posts
- 11. Carlos Manzo 320K posts
- 12. #Worlds2025 46.4K posts
- 13. Baseball 175K posts
- 14. #T1WIN 15.5K posts
- 15. Kendrick 19.3K posts
- 16. Ohtani 94.9K posts
- 17. Vladdy 23.6K posts
- 18. Mets 11.9K posts
- 19. Kershaw 40.4K posts
- 20. Dave Roberts 15.5K posts
Something went wrong.
Something went wrong.