From f464a5a26879ce34accf205dc5727b4e1361658e Mon Sep 17 00:00:00 2001 From: Priyansh <30593201+luciferreeves@users.noreply.github.com> Date: Tue, 27 Jul 2021 14:50:15 +0530 Subject: Create 2021-07-27-do-you-really-know-javascript.md --- _posts/2021-07-27-do-you-really-know-javascript.md | 234 +++++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 _posts/2021-07-27-do-you-really-know-javascript.md diff --git a/_posts/2021-07-27-do-you-really-know-javascript.md b/_posts/2021-07-27-do-you-really-know-javascript.md new file mode 100644 index 0000000..32c1a80 --- /dev/null +++ b/_posts/2021-07-27-do-you-really-know-javascript.md @@ -0,0 +1,234 @@ +--- +layout: post +title: "Most Beautiful Question of Computer Science (+Math)" +date: 2021-07-27 00:00:00 +0530 +author: Priyansh +tags: programming javascript computer-science +usemathjax: false +--- + +A lot has changed in the world of JavaScript – whether you are a newbie or an expert in JavaScript, you might not be able to get a hold on all the things that are possible in JavaScript. JavaScript is a lot more than you think and having some nifty ES6 tricks on your fingers could really save you both time and code complexity. + +I'm going to cover a few things you should be absolutely be using on a daily basis while you are programming in JavaScript and if you are serious about a future in JavaScript development, you will find these a really good add-on to your current knowledge. So, in order to explain things a lot better, we are going to cover this lesson through two versions of examples – one, the way people regularly write code – which is correct but inefficient and the second, the awesome way. + +So, let's start! + +## 1. The Spread Operator + +Okay, here's the task we wan't to accomplish - There are two arrays called 'array1' and 'array2' (Not so good names but I wasn't able to come up with something really good. So, I guess that's a drat!) We want to push all the elements of array1 to array2. So, how do we achieve it? + +### The Amatuer Way - + +```js +let array1 = ["one", "two"]; +let array2 = ["three", "four"]; + +for(var i=0; i { + cb('done') + }, 3000) +} + +doSomething((arg) => { + console.log('done here', arg); +}) +``` + +We used this to handle the fact that some operations were asynchronous and simply took time to finish. Then we got promise libraries that people started using and eventually, we got native support in the language. So now we can do things like: + +### A Little Better But Still Amatuer Way - + +```js +function doSomething() { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve('done') + }, 3000) + }) +} + +doSomething().then(arg => { + console.log('done here', arg); +}) +``` + +We can even chain the whole experience so we can do calls like this: + +```js +getUser() + .then(getOrderByUser) + .then(getOrderItemsByOrder) + .then(orderItems => { + // do something with order items + }) +``` + +Then we got **async/await** and life became even more glorious. Consider the above example with **Promises** now becoming this: + +### The Awesome Way - + +```js +async function getItems() { + try { + const user = await getUser(); + const order = await getOrderByUser(user); + const items = await getOrderItemsByOrder(order); + return items; + } catch(err) { + // handle error here, the suggestion to return something or rethrow + } +} + +getItems().then(items => { + // do something with order items +}) +``` + +And we get a synchronous-looking asynchronous code. Heck, yeah! :) + +## Summary + +There are more things I could mention about **ES6** and forward but I just wanted to show you a few of my favorites. I hope you have learnt something great today that I think you should adopt and bring these into practice. I also hope you continue to expand your knowledge of JavaScript and create something great someday! + +Well, that's it for this article guys! If you found this information helpful, share it with your friends on different social media platforms. It really motivates me to write more. And If you REALLY liked the article, consider [buying me a coffee](https://ko-fi.com/luciferreeves) 😊 -- cgit v1.2.3