The Array.prototype.findIndex() and Array.prototype.find() methods are useful to search the provided array. The findIndex() method returns the index of the first element in the array that satisfies the provided testing […]
JavaScript – Optional chaining (?.)
Optional chaining allows you to safely access the nested object properties. In general, to access the nested object properties you must and should check the object and it exists otherwise you may get a type error. Using […]
JavaScript – Nullish coalescing operator (??)
The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
1 2 3 4 5 6 7 8 9 10 11 |
const foo = null ?? 'default string'; console.log(foo); // expected output: "default string" const baz = 0 ?? 42; console.log(baz); // expected output: 0 const bar = undefined ?? 'default string'; console.log(bar); // expected output: default string |
JavaScript – Logical assignment operators
Logical assignment operators are new compound assignment operators that combine the logical operations &&, ||, or ?? with assignment
1 2 3 4 5 6 |
x &&= y; // Roughly equivalent to x && (x = y) x ||= y; // Roughly equivalent to x || (x = y) x ??= y; // Roughly equivalent to x ?? (x = y) |
Javascript – replaceAll
String.prototype.replaceAll provides an easy way to replace all occurrences of a substring without creating a global RegExp.
1 2 3 4 5 6 7 8 9 |
const queryString = 'q=query+string+parameters'; // Works, but requires escaping inside regular expressions. queryString.replace(/\+/g, ' '); // → 'q=query string parameters' // Simpler! queryString.replaceAll('+', ' '); // → 'q=query string parameters' |
Javascript – Promise.any
Promise.any is a promise combinator that resolves the resulting promise as soon as one of the input promises is fulfilled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const promises = [ fetch('/endpoint-a').then(() => 'a'), fetch('/endpoint-b').then(() => 'b'), fetch('/endpoint-c').then(() => 'c'), ]; try { const first = await Promise.any(promises); // Any of the promises was fulfilled. console.log(first); // → e.g. 'b' } catch (error) { // All of the promises were rejected. console.assert(error instanceof AggregateError); // Log the rejection values: console.log(error.errors); } |
Node.js 12: Private Class Fields
Javascript allows us to declare fields inside the class. We can access its own fields or properties by creating an instance of a class. Example:
1 2 3 4 5 |
Class Devreto { postId = 1; } const devreto = new Devreto(); devreto.postId ; //1 |
How to deploy angular universal to production
This post will cover the step needed to take the angular universal app to the production server. This post assumes that you have running angular universal application and which is generated by angular CLI. Now open your […]
How to Remove Duplicates from JavaScript Array
You can use the JavaScript built in object Set to keep unique values of any type. You can make use of Set to remove duplicate entries from a JavaScript array. Here is how the code looks
1 2 3 |
let technologies = ['JS','PHP','Ruby','Node','Python','PHP','JS'] technologies = [...new Set(technologies)] console.log(technologies) |
Async Await Error handling
In this post, I will show how to add error handling when using async/await. One of the goals of async/await is to make the asynchronous code appear more syntactically similar to synchronous code. This is also true for […]