diff --git a/blog_posts/4-javascript-array-methods.md b/blog_posts/4-javascript-array-methods.md index 02963344b..be416158b 100644 --- a/blog_posts/4-javascript-array-methods.md +++ b/blog_posts/4-javascript-array-methods.md @@ -9,7 +9,7 @@ excerpt: JavaScript arrays have a very robust API offering a plethora of amazing JavaScript arrays have a very robust API offering a plethora of amazing tools. Here are our top 4 JavaScript array methods every developer should know: -**Array.prototype.map()** +### Array.prototype.map() [`Array.prototype.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) creates a new array by applying the provided transformation to each element of the original array. The result is an array with the same length as the original array and elements transformed based on the provided function. @@ -19,7 +19,7 @@ const double = x => x * 2; arr.map(double); // [2, 4, 6] ``` -**Array.prototype.filter()** +### Array.prototype.filter() [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) creates a new array by using a filtering function to keep only elements that return `true` based on that function. The result is an array with equal or less than the original array's length, containing a subset of the same elements as the original array. @@ -31,7 +31,7 @@ arr.filter(isOdd); // [1, 3] ![JavaScript Array Methods](./blog_images/js-array-methods.png) -**Array.prototype.reduce()** +### Array.prototype.reduce() [`Array.prototype.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) creates an output value of any type depending on a reducer function and an initial value. The result can be of any type such as an integer, an object or an array, based on the reducer function provided. @@ -45,7 +45,7 @@ const increment = (x, y) => [...x, x[x.length - 1] + y]; arr.reduce(increment, [0]); // [0, 1, 3, 6] ``` -**Array.prototype.find()** +### Array.prototype.find() [`Array.prototype.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) returns the first element for which a matcher function returns `true`. The result is a single element from the original array. diff --git a/blog_posts/6-javascript-regexp-tricks.md b/blog_posts/6-javascript-regexp-tricks.md index 766358ede..f4ed53855 100644 --- a/blog_posts/6-javascript-regexp-tricks.md +++ b/blog_posts/6-javascript-regexp-tricks.md @@ -9,7 +9,7 @@ excerpt: Regular expressions, while very powerful, are notoriously hard to maste Regular expressions, while very powerful, are notoriously hard to master. Here are 6 useful features that can help you start using them in your JavaScript projects: -**Capturing groups** +### Capturing groups Capturing groups allow you to get specific parts of the matched string, simply by wrapping part of the regular expression in parentheses `(...)`: @@ -25,7 +25,7 @@ const str = 'JavaScript is a programming language'; */ ``` -**Non-capturing groups** +### Non-capturing groups Non-capturing groups are used for matching something without capturing it, like an either/or matching group that you do not really need. They are defined similarly to capturing groups, but prefixed with `?:`: @@ -40,7 +40,7 @@ const str = 'JavaScript is a programming language'; */ ``` -**Named capturing groups** +### Named capturing groups Named capturing groups allow you to name a capturing group, by prefixing it with ``: @@ -60,7 +60,7 @@ const str = 'JavaScript is a programming language'; */ ``` -**Capturing group backreferences** +### Capturing group backreferences Backreferences help you write shorter regular expressions, by repeating an existing capturing group, using `\1`, `\2` etc. Similarly, you can also repeat named capturing groups using `\k`: @@ -80,7 +80,7 @@ const str = 'JavaScript is a programming language - an awesome programming langu */ ``` -**Lookaheads** +### Lookaheads Lookaheads allow you to check if something is followed by a certain pattern, without actually matching it. You can create positive lookaheads using `?=` and negative lookaheads using `?!`: @@ -103,7 +103,7 @@ const str = 'JavaScript is not the same as Java and you should remember that'; */ ``` -**Unicode characters** +### Unicode characters Finally, you can match unicode characters, using `/p{...}` and the `/u` flag. Examples include, but are not limited to `{Emoji}`, `{Math_Symbols}` and `{Script=Greek}`: diff --git a/blog_posts/async-javascript-cheatsheet.md b/blog_posts/async-javascript-cheatsheet.md index f06c78f88..b3ae68d71 100644 --- a/blog_posts/async-javascript-cheatsheet.md +++ b/blog_posts/async-javascript-cheatsheet.md @@ -7,13 +7,13 @@ cover: blog_images/green-plant.jpg excerpt: Learn everything you need to know about promises and asynchronous JavaScript with this handy cheatsheet. --- -**Promise basics** +### Promise basics - **Promises** start in a **pending state**, neither fullfiled or rejected. - When the operation is completed, a promise will become **fullfiled with a value**. - If the operation fails, a promise will get **rejected with an error**. -**Creating promises** +### Creating promises - The function passed to a `new Promise` will execute synchronously. - Use `resolve()` or `reject()` to create promises from values. @@ -34,7 +34,7 @@ new Promise((resolve, reject) => { const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); ``` -**Handling promises** +### Handling promises - `Promise.prototype.then()` accepts two optional arguments (`onFulfilled`, `onRejected`). - `Promise.prototype.then()` will call `onFulfilled` once the promise is fulfilled. @@ -68,7 +68,7 @@ promisedOperation() - All three of the above methods will not be executed at least until the next tick, even for promises that already have an outcome. -**Combining promises** +### Combining promises - `Promise.all()` turns an array of promises into a promise of an array. - If any promise is rejected, the error will pass through. @@ -89,7 +89,7 @@ Promise }); ``` -**async/await** +### async/await - Calling an `async` function always results in a promise. - `(async () => value)()` will resolve to `value`. diff --git a/blog_posts/code-anatomy-chaining-reduce-for-loop.md b/blog_posts/code-anatomy-chaining-reduce-for-loop.md index f36e5fa93..3e39379f9 100644 --- a/blog_posts/code-anatomy-chaining-reduce-for-loop.md +++ b/blog_posts/code-anatomy-chaining-reduce-for-loop.md @@ -7,7 +7,7 @@ cover: blog_images/code-anatomy-optimizing-recursion.jpg excerpt: There are many ways to iterate and transform array data in JavaScript. Learn how each one works and where you should use them. --- -**For loops** +### For loops ```js const files = [ 'foo.txt ', '.bar', ' ', 'baz.foo' ]; @@ -31,7 +31,7 @@ for (let file of files) { - Uses `Array.prototype.push()` or the spread (`...`) operator to add elements. - `O(N)` complexity, each element will be iterated over only once. -**Array reduce** +### Array reduce ```js const files = [ 'foo.txt ', '.bar', ' ', 'baz.foo' ]; @@ -54,7 +54,7 @@ const filePaths = files.reduce((acc, file) => { - Uses `Array.prototype.push()` or the spread (`...`) operator to add elements. - `O(N)` complexity, each element will be iterated over only once. -**Method chaining** +### Method chaining ```js const files = [ 'foo.txt ', '.bar', ' ', 'baz.foo' ]; diff --git a/blog_posts/code-anatomy-optimizing-recursion.md b/blog_posts/code-anatomy-optimizing-recursion.md index 015a4d2ee..75ff35ab4 100644 --- a/blog_posts/code-anatomy-optimizing-recursion.md +++ b/blog_posts/code-anatomy-optimizing-recursion.md @@ -7,7 +7,7 @@ cover: blog_images/code-anatomy-optimizing-recursion.jpg excerpt: Recursive code has a tendency of being inefficient and can leave a lot of space for optimization. Learn a couple of tricks we use to speed up our recursive functions. --- -**Recursive functions** +### Recursive functions Recursion is a programming technique where the final solution is computed by breaking down the problem into smaller instances of the same problem and computing the solution for each one. The most common implementation is a function that calls itself, reducing the problem every time until it reaches an instance of the problem whose solution is either trivial to compute or already known. Let's look at a very well-known example, calculating the `n`th term of the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), implemented using recursion in JavaScript: @@ -49,7 +49,7 @@ fibonacciNumber(4); As you can see, for each value of `n`, `fibonacciNumber` will be called twice, once with `n - 1` and once with `n - 2` and this will continue until it's called with either `1` or `0`. While this is straightforward to write and understand, it is inefficient as it will have to calculate the same value more than once. -**Calculation memoization** +### Calculation memoization The solution to this problem, and the first trick that you can use to speed up recursive functions, is to use memoization. We already published [a great blog post on memoization](https://www.30secondsofcode.org/blog/s/javascript-memoization/) a little while back, so be sure to check it out to learn more about the subject. Here's our `fibonacciNumber` function, using memoization: @@ -91,7 +91,7 @@ fibonacciNumber(4); As you can see in the example above, the value for each `n` is only computed once. While the Fibonacci sequence doesn't require any costly calculations, this could make a huge difference for a more computationally expensive problem. It will also be a lot more noticable for higher values of `n` where the number of calculations will increase singificantly. -**Using iteration** +### Using iteration The second and final trick stems from the very definition of recursive programming turned on its head. If we can solve a smaller instance of the problem and use it for the solution of a larger instance of the problem, it should be possible to work iteratively from the smaller problem to the larger one, instead of recursively. Here's this idea in practice for our `fibonacciNumber` function: diff --git a/blog_posts/cookies-local-storage-session.md b/blog_posts/cookies-local-storage-session.md index d8c75a8c9..497600a42 100644 --- a/blog_posts/cookies-local-storage-session.md +++ b/blog_posts/cookies-local-storage-session.md @@ -7,7 +7,7 @@ cover: blog_images/three-vases.jpg excerpt: Learn the difference between cookies, local storage and session storage and start using the correct option for your needs. --- -**Cookies** +### Cookies Cookies store small amounts of data that has to be sent back to the server with subsequent requests and their expiration can be set from either server or client. They are primarily used for server-side reading. @@ -19,7 +19,7 @@ Cookies store small amounts of data that has to be sent back to the server with - Blockable by users: Yes - Editable by users: Yes -**Local storage** +### Local storage Local storage stores a larger amount of data on the client's computer in a key-value pair format and has no expiration date. Data is never transferred to the server and is accesible via JavaScript and HTML5. @@ -31,7 +31,7 @@ Local storage stores a larger amount of data on the client's computer in a key-v - Blockable by users: Yes - Editable by users: Yes -**Session storage** +### Session storage Session storage stores a larger amount of data on the client's computer only for the current session, expiring the data on tab close. Data is never transferred to the server and is accessible client-side from the same tab. diff --git a/blog_posts/copy-text-to-clipboard-with-javascript.md b/blog_posts/copy-text-to-clipboard-with-javascript.md index 1e96ada5d..351c5ea40 100644 --- a/blog_posts/copy-text-to-clipboard-with-javascript.md +++ b/blog_posts/copy-text-to-clipboard-with-javascript.md @@ -7,7 +7,7 @@ cover: blog_images/copy-text-to-clipboard-with-javascript.jpg excerpt: Learn how to programmatically copy text to clipboard with a few lines of JavaScript and level up your web development skills. --- -**Core functionality** +### Core functionality A very common need when building websites is the ability to copy text to clipboard with a single button click. Javascript can easily do this in five short steps:hout the user selecting it or hitting the appropriate key combination on their keyboard. Javascript can easily do this in five short steps: @@ -32,7 +32,7 @@ const copyToClipboard = str => { Bear in mind that this method will not work everywhere, but only as a result of a user action (e.g. inside a `click` event listener), due to the way `Document.execCommand()` works. -**Hide the appended element** +### Hide the appended element The above method, while functional, might have some issues such as flashing when appending and removing the `