├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nick Scialli 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
5 |
6 | A continuously-evolving compendium of javascript tips based on common areas of confusion or misunderstanding. 7 |
8 | 9 | --- 10 | 11 | **Want to learn more about JavaScript development? Consider:** 12 | - Signing up for my [free newsletter](https://howthewebworks.substack.com) where I explain how the web works! 13 | - Subscribing to my [free youtube channel](https://www.youtube.com/c/devtutsco) where I teach JavaScript, Typescript, and React tutorials! 14 | 15 | --- 16 | 17 | 18 | ## Contents 19 | 20 | - [Value vs. Reference Variable Assignment](#value-vs-reference-variable-assignment) 21 | - [Closures](#closures) 22 | - [Destructuring](#destructuring) 23 | - [Spread Syntax](#spread-syntax) 24 | - [Rest Syntax](#rest-syntax) 25 | - [Array Methods](#array-methods) 26 | - [Generators](#generators) 27 | - [Identity Operator (===) vs. Equality Operator (==)](#identity-operator--vs-equality-operator) 28 | - [Object Comparison](#object-comparison) 29 | - [Callback Functions](#callback-functions) 30 | - [Promises](#promises) 31 | - [Async Await](#async-await) 32 | - [DOM Manipulation](#dom-manipulation) 33 | - [Interview Questions](#interview-questions) 34 | - [Miscellaneous](#miscellaneous) 35 | 36 | ## Value vs. Reference Variable Assignment 37 | 38 | Understanding how JavaScript assigns to variables is foundational to writing bug-free JavaScript. If you don't understand this, you could easily write code that unintentionally changes values. 39 | 40 | When JavaScript assigns one of the seven primitive type (i.e., Boolean, Null, Undefined, String, Number, Symbol, and BigInt.) to a variable, the JavaScript runtime gets to determine whether that primitive is assigned by *reference* or by *value*. It doesn't really matter how it's done because primitives can't be mutated (they're *immutable*). However, when the assigned value is an `Array`, `Function`, or `Object` a reference to the array/function/object in memory is assigned. 41 | 42 | Example time! In the following snippet, `var2` is set as equal to `var1`. Since `var1` is a primitive type (`String`), `var2` is set as equal to `var1`'s String value and can be thought of as completely distinct from `var1` at this point. Accordingly, reassigning `var2` has no effect on `var1`. 43 | 44 | ```javascript 45 | const var1 = 'My string'; 46 | let var2 = var1; 47 | 48 | var2 = 'My new string'; 49 | 50 | console.log(var1); 51 | // 'My string' 52 | console.log(var2); 53 | // 'My new string' 54 | ``` 55 | 56 | Let's compare this with object assignment. 57 | 58 | ```javascript 59 | const var1 = { name: 'Jim' }; 60 | const var2 = var1; 61 | 62 | var2.name = 'John'; 63 | 64 | console.log(var1); 65 | // { name: 'John' } 66 | console.log(var2); 67 | // { name: 'John' } 68 | ``` 69 | 70 | How this is working: 71 | - The object `{ name: 'Jim' }` is created in memory 72 | - The variable `var1` is assigned a *reference* to the created object 73 | - The variable `var2` is set to equal `var1`... which is a reference to that same object in memory! 74 | - `var2` is mutated, which really means *the object var2 is referencing is mutated* 75 | - `var1` is pointing to the same object as `var2`, and therefore we see this mutation when accessing `var1` 76 | 77 | One might see how this could cause problems if you expected behavior like primitive assignment! This can get especially ugly if you create a function that unintentionally mutates an object. 78 | 79 | For more on variable assignment and primitive/object mutability, see this article. 80 | 81 | ## Closures 82 | 83 | Closure is an important javascript pattern to give private access to a variable. In this example, `createGreeter` returns an anonymous function that has access to the supplied `greeting`, "Hello." For all future uses, `sayHello` will have access to this greeting! 84 | 85 | ```javascript 86 | function createGreeter(greeting) { 87 | return function(name) { 88 | console.log(greeting + ', ' + name); 89 | }; 90 | } 91 | 92 | const sayHello = createGreeter('Hello'); 93 | 94 | sayHello('Joe'); 95 | // Hello, Joe 96 | ``` 97 | 98 | In a more real-world scenario, you could envision an initial function `apiConnect(apiKey)` that returns some methods that would use the API key. In this case, the `apiKey` would just need to be provided once and never again. 99 | 100 | ```javascript 101 | function apiConnect(apiKey) { 102 | function get(route) { 103 | return fetch(`${route}?key=${apiKey}`); 104 | } 105 | 106 | function post(route, params) { 107 | return fetch(route, { 108 | method: 'POST', 109 | body: JSON.stringify(params), 110 | headers: { 111 | Authorization: `Bearer ${apiKey}` 112 | } 113 | }); 114 | } 115 | 116 | return { get, post }; 117 | } 118 | 119 | const api = apiConnect('my-secret-key'); 120 | 121 | // No need to include the apiKey anymore 122 | api.get('http://www.example.com/get-endpoint'); 123 | api.post('http://www.example.com/post-endpoint', { name: 'Joe' }); 124 | ``` 125 | 126 | ## Destructuring 127 | 128 | Don't be thrown off by javascript parameter destructuring! It's a common way to cleanly extract properties from objects. 129 | 130 | ```javascript 131 | const obj = { 132 | name: 'Joe', 133 | food: 'cake' 134 | }; 135 | 136 | const { name, food } = obj; 137 | 138 | console.log(name, food); 139 | // 'Joe' 'cake' 140 | ``` 141 | 142 | If you want to extract properties under a different name, you can specify them using the following format. 143 | 144 | ```javascript 145 | const obj = { 146 | name: 'Joe', 147 | food: 'cake' 148 | }; 149 | 150 | const { name: myName, food: myFood } = obj; 151 | 152 | console.log(myName, myFood); 153 | // 'Joe' 'cake' 154 | ``` 155 | 156 | In the following example, destructuring is used to cleanly pass the `person` object to the `introduce` function. In other words, destructuring can be (and often is) used directly for extracting parameters passed to a function. If you're familiar with React, you probably have seen this before! 157 | 158 | ```javascript 159 | const person = { 160 | name: 'Eddie', 161 | age: 24 162 | }; 163 | 164 | function introduce({ name, age }) { 165 | console.log(`I'm ${name} and I'm ${age} years old!`); 166 | } 167 | 168 | introduce(person); 169 | // "I'm Eddie and I'm 24 years old!" 170 | ``` 171 | 172 | ## Spread Syntax 173 | 174 | A javascript concept that can throw people off but is relatively simple is the spread operator! In the following case, `Math.max` can't be applied to the `arr` array because it doesn't take an array as an argument, it takes the individual elements as arguments. The spread operator `...` is used to pull the individual elements out the array. 175 | 176 | ```javascript 177 | const arr = [4, 6, -1, 3, 10, 4]; 178 | const max = Math.max(...arr); 179 | console.log(max); 180 | // 10 181 | ``` 182 | 183 | ## Rest Syntax 184 | 185 | Let's talk about javascript rest syntax. You can use it to put any number of arguments passed to a function into an array! 186 | 187 | ```javascript 188 | function myFunc(...args) { 189 | console.log(args[0] + args[1]); 190 | } 191 | 192 | myFunc(1, 2, 3, 4); 193 | // 3 194 | ``` 195 | 196 | ## Array Methods 197 | 198 | JavaScript array methods can often provide you incredible, elegant ways to perform the data transformation you need. As a contributor to StackOverflow, I frequently see questions regarding how to manipulate an array of objects in one way or another. This tends to be the perfect use case for array methods. 199 | 200 | I will cover a number of different array methods here, organized by similar methods that sometimes get conflated. This list is in no way comprehensive: I encourage you to review and practice all of them discussed on MDN (my favorite JavaScript reference). 201 | 202 | ### map, filter, reduce 203 | 204 | There is some confusion around the javascript array methods `map`, `filter`, `reduce`. These are helpful methods for transforming an array or returning an aggregate value. 205 | 206 | - **map:** return array where each element is transformed as specified by the function 207 | 208 | ```javascript 209 | const arr = [1, 2, 3, 4, 5, 6]; 210 | const mapped = arr.map(el => el + 20); 211 | console.log(mapped); 212 | // [21, 22, 23, 24, 25, 26] 213 | ``` 214 | 215 | - **filter:** return array of elements where the function returns true 216 | 217 | ```javascript 218 | const arr = [1, 2, 3, 4, 5, 6]; 219 | const filtered = arr.filter(el => el === 2 || el === 4); 220 | console.log(filtered); 221 | // [2, 4] 222 | ``` 223 | 224 | - **reduce:** accumulate values as specified in function 225 | 226 | ```javascript 227 | const arr = [1, 2, 3, 4, 5, 6]; 228 | const reduced = arr.reduce((total, current) => total + current, 0); 229 | console.log(reduced); 230 | // 21 231 | ``` 232 | 233 | _Note:_ It is always advised to specify an _initialValue_ or you could receive an error. For example: 234 | 235 | ```javascript 236 | const arr = []; 237 | const reduced = arr.reduce((total, current) => total + current); 238 | console.log(reduced); 239 | // Uncaught TypeError: Reduce of empty array with no initial value 240 | ``` 241 | 242 | _Note:_ If there’s no initialValue, then reduce takes the first element of the array as the initialValue and starts the iteration from the 2nd element 243 | 244 | You can also read this [tweet](https://twitter.com/sophiebits/status/1099014182261776384?s=20) by Sophie Alpert (@sophiebits), when it is recommended to usereduce
245 |
246 | ### find, findIndex, indexOf
247 |
248 | The array methods `find`, `findIndex`, and `indexOf` can often be conflated. Use them as follows.
249 |
250 | - **find:** return the first instance that matches the specified criteria. Does not progress to find any other matching instances.
251 |
252 | ```javascript
253 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
254 | const found = arr.find(el => el > 5);
255 | console.log(found);
256 | // 6
257 | ```
258 |
259 | Again, note that while everything after 5 meets the criteria, only the first matching element is returned. This is actually super helpful in situations where you would normally break a `for` loop when you find a match!
260 |
261 | - **findIndex:** This works almost identically to find, but rather than returning the first matching element it returns the index of the first matching element. Take the following example, which uses names instead of numbers for clarity.
262 |
263 | ```javascript
264 | const arr = ['Nick', 'Frank', 'Joe', 'Frank'];
265 | const foundIndex = arr.findIndex(el => el === 'Frank');
266 | console.log(foundIndex);
267 | // 1
268 | ```
269 |
270 | - **indexOf:** Works almost identically to findIndex, but instead of taking a function as an argument it takes a simple value. You can use this when you have simpler logic and don't need to use a function to check whether there is a match.
271 |
272 | ```javascript
273 | const arr = ['Nick', 'Frank', 'Joe', 'Frank'];
274 | const foundIndex = arr.indexOf('Frank');
275 | console.log(foundIndex);
276 | // 1
277 | ```
278 |
279 | ### push, pop, shift, unshift
280 |
281 | There are a lot of great array method to help add or remove elements from arrays in a targeted fashion.
282 |
283 | - **push:** This is a relatively simple method that adds an item to the end of an array. It modifies the array in-place and the function itself returns the length of the new array.
284 |
285 | ```javascript
286 | const arr = [1, 2, 3, 4];
287 | const pushed = arr.push(5);
288 | console.log(arr);
289 | // [1, 2, 3, 4, 5]
290 | console.log(pushed);
291 | // 5
292 | ```
293 |
294 | - **pop:** This removes the last item from an array. Again, it modifies the array in place. The function itself returns the item removed from the array.
295 |
296 | ```javascript
297 | const arr = [1, 2, 3, 4];
298 | const popped = arr.pop();
299 | console.log(arr);
300 | // [1, 2, 3]
301 | console.log(popped);
302 | // 4
303 | ```
304 |
305 | - **shift:** This removes the first item from an array. Again, it modifies the array in place. The function itself returns the item removed from the array.
306 |
307 | ```javascript
308 | const arr = [1, 2, 3, 4];
309 | const shifted = arr.shift();
310 | console.log(arr);
311 | // [2, 3, 4]
312 | console.log(shifted);
313 | // 1
314 | ```
315 |
316 | - **unshift:** This adds one or more elements to the beginning of an array. Again, it modifies the array in place. Unlike a lot of the other methods, the function itself returns the new length of the array.
317 |
318 | ```javascript
319 | const arr = [1, 2, 3, 4];
320 | const unshifted = arr.unshift(5, 6, 7);
321 | console.log(arr);
322 | // [5, 6, 7, 1, 2, 3, 4]
323 | console.log(unshifted);
324 | // 7
325 | ```
326 |
327 | ### splice, slice
328 |
329 | These methods either modify or return subsets of arrays.
330 |
331 | - **splice:** Change the contents of an array by removing or replacing existing elements and/or adding new elements. This method modifies the array in place.
332 |
333 | ```javascript
334 | The following code sample can be read as: at position 1 of the array, remove 0 elements and insert b.
335 | const arr = ['a', 'c', 'd', 'e'];
336 | arr.splice(1, 0, 'b');
337 | console.log(arr);
338 | // ['a', 'b', 'c', 'd', 'e']
339 | ```
340 |
341 | - **slice:** returns a shallow copy of an array from a specified start position and before a specified end position. If no end position is specified, the rest of the array is returned. Importantly, this method does not modify the array in place but rather returns the desired subset.
342 |
343 | ```javascript
344 | const arr = ['a', 'b', 'c', 'd', 'e'];
345 | const sliced = arr.slice(2, 4);
346 | console.log(sliced);
347 | // ['c', 'd']
348 | console.log(arr);
349 | // ['a', 'b', 'c', 'd', 'e']
350 | ```
351 |
352 | ### sort
353 |
354 | - **sort:** sorts an array based on the provided function which takes a first element and second element argument. Modifies the array in place. If the function returns negative or 0, the order remains unchanged. If positive, the element order is switched.
355 |
356 | ```javascript
357 | const arr = [1, 7, 3, -1, 5, 7, 2];
358 | const sorter = (firstEl, secondEl) => firstEl - secondEl;
359 | arr.sort(sorter);
360 | console.log(arr);
361 | // [-1, 1, 2, 3, 5, 7, 7]
362 | ```
363 |
364 | Phew, did you catch all of that? Neither did I. In fact, I had to reference the MDN docs a lot while writing this - and that's okay! Just knowing what kind of methods are out there with get you 95% of the way there.
365 |
366 | ## Generators
367 |
368 | Don't fear the `*`. The generator function specifies what `value` is yielded next time `next()` is called. Can either have a finite number of yields, after which `next()` returns an `undefined` value, or an infinite number of values using a loop.
369 |
370 | ```javascript
371 | function* greeter() {
372 | yield 'Hi';
373 | yield 'How are you?';
374 | yield 'Bye';
375 | }
376 |
377 | const greet = greeter();
378 |
379 | console.log(greet.next().value);
380 | // 'Hi'
381 | console.log(greet.next().value);
382 | // 'How are you?'
383 | console.log(greet.next().value);
384 | // 'Bye'
385 | console.log(greet.next().value);
386 | // undefined
387 | ```
388 |
389 | And using a generator for infinite values:
390 |
391 | ```javascript
392 | function* idCreator() {
393 | let i = 0;
394 | while (true) yield i++;
395 | }
396 |
397 | const ids = idCreator();
398 |
399 | console.log(ids.next().value);
400 | // 0
401 | console.log(ids.next().value);
402 | // 1
403 | console.log(ids.next().value);
404 | // 2
405 | // etc...
406 | ```
407 |
408 | ## Identity Operator (===) vs. Equality Operator (==)
409 |
410 | Be sure to know the difference between the identify operator (`===`) and equality operator (`==`) in javascript! The `==` operator will do type conversion prior to comparing values whereas the `===` operator will not do any type conversion before comparing.
411 |
412 | ```javascript
413 | console.log(0 == '0');
414 | // true
415 | console.log(0 === '0');
416 | // false
417 | ```
418 |
419 | ## Object Comparison
420 |
421 | A mistake I see javascript newcomers make is directly comparing objects. Variables are pointing to references to the objects in memory, not the objects themselves! One method to actually compare them is converting the objects to JSON strings. This has a drawback though: `JSON.stringify` is not able to stringify a lot of object types (e.g., functions and sets)! A safer way to compare objects is to pull in a library that specializes in deep object comparison (e.g., lodash's isEqual).
422 |
423 | The following objects appear equal but they are in fact pointing to different references.
424 |
425 | ```javascript
426 | const joe1 = { name: 'Joe' };
427 | const joe2 = { name: 'Joe' };
428 |
429 | console.log(joe1 === joe2);
430 | // false
431 | ```
432 |
433 | Conversely, the following evaluates as true because one object is set equal to the other object and are therefore pointing to the same reference (there is only one object in memory).
434 |
435 | ```javascript
436 | const joe1 = { name: 'Joe' };
437 | const joe2 = joe1;
438 |
439 | console.log(joe1 === joe2);
440 | // true
441 | ```
442 |
443 | Make sure to review the Value vs. Reference section above to fully understand the ramifications of setting a variable equal to another variable that's pointing to a reference to an object in memory!
444 |
445 | ## Callback Functions
446 |
447 | Far too many people are intimidated by javascript callback functions! They are simple, take this example. The `console.log` function is being passed as a callback to `myFunc`. It gets executed when `setTimeout` completes. That's all there is to it!
448 |
449 | ```javascript
450 | function myFunc(text, callback) {
451 | setTimeout(function() {
452 | callback(text);
453 | }, 2000);
454 | }
455 |
456 | myFunc('Hello world!', console.log);
457 | // 'Hello world!'
458 | ```
459 |
460 | ## Promises
461 |
462 | Once you understand javascript callbacks you'll soon find yourself in nested "callback hell." This is where Promises help! Wrap your async logic in a `Promise` and `resolve` on success or `reject` on fail. Use `then` to handle success and `catch` to handle failure.
463 |
464 | ```javascript
465 | const myPromise = new Promise(function(res, rej) {
466 | setTimeout(function() {
467 | if (Math.random() < 0.9) {
468 | return res('Hooray!');
469 | }
470 | return rej('Oh no!');
471 | }, 1000);
472 | });
473 |
474 | myPromise
475 | .then(function(data) {
476 | console.log('Success: ' + data);
477 | })
478 | .catch(function(err) {
479 | console.log('Error: ' + err);
480 | });
481 |
482 | // If Math.random() returns less than 0.9 the following is logged:
483 | // "Success: Hooray!"
484 | // If Math.random() returns 0.9 or greater the following is logged:
485 | // "Error: Oh no!"
486 | ```
487 |
488 | ### Avoid the nesting anti-pattern of promise chaining!
489 |
490 | `.then` methods can be chained. I see a lot of new comers end up in some kind of call back hell inside of a promise when it's completely unnecessary.
491 |
492 | ```javascript
493 | //The wrong way
494 | getSomedata.then(data => {
495 | getSomeMoreData(data).then(newData => {
496 | getSomeRelatedData(newData => {
497 | console.log(newData);
498 | });
499 | });
500 | });
501 | ```
502 |
503 | ```javascript
504 | //The right way
505 | getSomeData
506 | .then(data => {
507 | return getSomeMoreData(data);
508 | })
509 | .then(data => {
510 | return getSomeRelatedData(data);
511 | })
512 | .then(data => {
513 | console.log(data);
514 | });
515 | ```
516 |
517 | You can see how it's much easier to read the second form and with ES6 implicit returns we could even simplify that further:
518 |
519 | ```javascript
520 | getSomeData
521 | .then(data => getSomeMoreData(data))
522 | .then(data => getSomeRelatedData(data))
523 | .then(data => console.log(data));
524 | ```
525 | Because the function supplied to .then will be called with the the result of the resolve method from the promise we can omit the ceremony of creating an anonymous function altogether. This is equivalent to above:
526 |
527 | ```javascript
528 | getSomeData
529 | .then(getSomeMoreData)
530 | .then(getSomeRelatedData)
531 | .then(console.log);
532 | ```
533 |
534 | ## Async Await
535 |
536 | Once you get the hang of javascript promises, you might like `async await`, which is just "syntactic sugar" on top of promises. In the following example we create an `async` function and within that we `await` the `greeter` promise.
537 |
538 | ```javascript
539 | const greeter = new Promise((res, rej) => {
540 | setTimeout(() => res('Hello world!'), 2000);
541 | });
542 |
543 | async function myFunc() {
544 | const greeting = await greeter;
545 | console.log(greeting);
546 | }
547 |
548 | myFunc();
549 | // 'Hello world!'
550 | ```
551 |
552 | ### Async functions return a promise
553 |
554 | One important thing to note here is that the result of an `async` function is a promise.
555 |
556 | ```javascript
557 | const greeter = new Promise((res, rej) => {
558 | setTimeout(() => res('Hello world!'), 2000);
559 | });
560 |
561 | async function myFunc() {
562 | return await greeter;
563 | }
564 |
565 | console.log(myFunc()); // => Promise {}
566 |
567 | myFunc().then(console.log); // => Hello world!
568 | ```
569 |
570 | ## DOM Manipulation
571 |
572 | ### Create Your Own Query Selector Shorthand
573 |
574 | When working with JS in the browser, instead of writing `document.querySelector()`/`document.querySelectorAll()` multiple times, you could do the following thing:
575 |
576 | ```javascript
577 | const $ = document.querySelector.bind(document);
578 | const $$ = document.querySelectorAll.bind(document);
579 |
580 | // Usage
581 | const demo = $('#demo');
582 | // Select all the `a` tags
583 | [...$$("a[href *='#']")].forEach(console.log);
584 | ```
585 |
586 | ## Interview Questions
587 |
588 | ### Traversing a Linked List
589 |
590 | Here's a javascript solution to a classic software development interview question: traversing a linked list. You can use a while loop to recursively iterate through the linked list until there are no more values!
591 |
592 | ```javascript
593 | const linkedList = {
594 | val: 5,
595 | next: {
596 | val: 3,
597 | next: {
598 | val: 10,
599 | next: null
600 | }
601 | }
602 | };
603 |
604 | const arr = [];
605 | let head = linkedList;
606 |
607 | while (head !== null) {
608 | arr.push(head.val);
609 | head = head.next;
610 | }
611 |
612 | console.log(arr);
613 | // [5, 3, 10]
614 | ```
615 |
616 | ## Miscellaneous
617 |
618 | ### Increment and Decrement
619 |
620 | Ever wonder what the difference between `i++` and `++i` was? Did you know both were options? `i++` returns `i` and then increments it whereas `++i` increments `i` and then returns it.
621 |
622 | ```javascript
623 | let i = 0;
624 | console.log(i++);
625 | // 0
626 | ```
627 |
628 | ```javascript
629 | let i = 0;
630 | console.log(++i);
631 | // 1
632 | ```
633 |
634 | # Contributing
635 |
636 | Contributions welcome! All I ask is that you open an issue and we discuss your proposed changes before you create a pull request.
637 |
--------------------------------------------------------------------------------