JS Tip - Required function parameters

Sometimes we need to ensure that a required parameter has been provided. This can be achieved by two easy ways - Default function parameters Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. We can take the advantages of this ES6 feature and make a function which throws an error and assign it as default value for required parameters. So when no parameter is passed this function will be called and it will thrown an error !!! /** * Using Default function parameter */ const required = () => { throw new TypeError("Missing paramter!!"); } const foo = (bar = required()) => console.log(bar) foo("Hello!!") //prints Hello!! foo() // will throw error - Global property Another way to deal with required function parameters is to create a global property named required . It can be assigned as default function parameter, so if no value has been specified

JS Tip - Use variable parameters for setTimeout()

setTimeout function allows us to sets a timer which executes a function or specified piece of code once the timer expires. Generally to call a function with parameter inside setTimeout we wrap the function inside anonymous function. But we can pass the function parameters directly !! const logger = message => { console.log(message); }; // Using anonymous function setTimeout(() => { logger("Hello!!"); }, 1000); // Pass parameter to callback function directly // setTimout(function, delay, ..args) setTimeout(logger, 1000, 'How are you?')

Optional Chaining & Nullish Coalescing in Javascript!!

Javascript soon be having most awaited operators Optional Chaining & Nullish Coalescing . Currently these two features are as proposals in v8 ( optional-chaining , nullish-coalescing ). These operators are already available to try on chrome canary. It means we are seeing them soon in the chrome'a stable release and Node.js. For example lets define a book object Now if we want to access the sale property then it might be error-prone, as any of them might evaluate to null or undefined (also known as “nullish” values). To safely access the sale property, Checking for property existence on each step easily turns into a deeply-nested structure of if-statements or a long if-condition replicating the property access chain. or According to a recent spec proposal, “an optional chain is a chain of one or more property accesses and function calls, the first of which begins with the token ?.” Using the new optional chainin

Disallow specific imports in Javascript Project

Have you been in   scenario  where team members are using different libraries to perform identical functionalities and resulting in a large JS  bundle? Well, recently our team faced the same scenario. We started a new project related to the  Ethereum block chain. To interact with the Ethereum Block Chain and its ecosystem there are couple of libraries available in market from which  ethers.js  and  web3js  are widely used. In our project we decided to use the former one. After the couple of days an inter joins us. We gave her a small task to start with,  so at the end of the day, she imported the web3.js performed the task and committed the changes !!!.  So here we have unnecessary import in project that will bloat our JS bundle o r there might  some scenario in which we may want to restrict import of a sub module or part of a large module. Well in such case ESLint  's  no-restricted-imports  rule can rescue us. Below is simple example of using  no-restricted-import  by de

Different methods to copy an object in JavaScript

In this post we will discuss the different methods to copy the javascript object and discuss the outcome of each method. But before that just keep one thing in mind that javascript objects are  mutable   by nature and stored as a reference. So when you assign the object to another variable, you're just assigning the memory address of the object to that variable. In this case your old and new object both will point to the same memory address. Any change in either object will be reflected in the other. So just assigning the one object to another will not really copies your object!!! // Define object bar var bar = { x: 'Hi' } console. log ( bar .x); //Hi // Assign it to foo var foo= bar ; console. log (foo.x); //Hi //But foo.x='Hello!! Im foo.'; console. log (foo.x); //Hello!! Im foo. console. log ( bar .x); //Hello!! Im foo. bar .x= "Nice to meet you foo!!" ; console. log (foo.x); //Nice to meet you foo!! console. log ( bar .x);