avatar

Gagandeep Singh

Create a globally executable javascript file

We will understand and see how to create an executable javascript file using a global command. Lets say for example you want to create a clone of ‘ls’ or ‘cmd’ command which shows the content of the current folder or from the path provided. To acheive that we need to have a global command. To acheive this we need to do couple of things and need to have 2 files (index.

Generics in Typescript

Tuples is a type that exists in Typescript which is similar to Array type but has fixed length and defined what kind of element (either same or different types) will be there at a certain position. // Array example const a: number[] = [1, 2, 3]; // Tuple example const t: [string, number, boolean] = ["2", 1, true]; Since Tuples are just arrays, so it will allow to push a new element only of type either string or number as defined in the tupleType below.

Tuples in Typescript

Tuples is a type that exists in Typescript which is similar to Array type but has fixed length and defined what kind of element (either same or different types) will be there at a certain position. // Array example const a: number[] = [1, 2, 3]; // Tuple example const t: [string, number, boolean] = ["2", 1, true]; Since Tuples are just arrays, so it will allow to push a new element only of type either string or number as defined in the TupleType below.

Using pre-commit hook in package.json

One of the traits of someone who follows or moving towards a good coding practice environment is making sure that you are not pushing the code which may have linting issues and/or failing unit tests. Adding a pre-commit hook forces to check the lint and run unit tests (or any other script) to be sure that everything is good before committing changes. npm i --save-dev pre-commit Next step is to update the package.

undefined and null in Javascript

undefined 🔗In Javascript undefined (a primitive type in Javascript) gets assigned to a variable that we declare but don’t assign a value to. It’s Javascript’s way of telling that the value of this variable is not defined. Even a function in Javascript which does not return anything, will return undefined by default. So, to avoid any error we should never assign undefined to a variable manually. typeof undefined; outputs: undefined; null 🔗Unlike undefined, null (another primitive type in Javascript) is used when we want to manually or intentionally set the value of a variable to be empty.