Posts

Showing posts from May 9, 2021

Create Extension Method in typescript - String.IsNullOrEmpty()

Image
How to Create Extension Method using Typescript in Angular Application - String.IsNullOrEmpty() Step 1 : Create typings.d.ts (declaration file) in the application and declare the method in interface like below. HTML interface StringConstructor { isNullOrEmpty(value: string): boolean; } Step 2 : Create extension.ts file in the application and implement the method like below. HTML String.isNullOrEmpty = function (value: string): boolean { return value === undefined || value.trim() === '' || value === null; }; Step 3 : Usage of the Method in the component or class file. HTML import './extensions'; export class TestComponent implements OnInit { public ngOnInit(): void { let abc; console.log(String.isNullOrEmpty(abc)); } } Step 4 : Output like below.

Javascript Hoisting

JavaScript hoisting is a mechanism where variable and function declarations are moved to the top of their respective scopes (either the global scope or function scope) during the compilation or interpretation phase, regardless of where they are declared in the source code. This means that even if a variable or function is declared later in the code, it can be used before its actual declaration. However, it is important to note that only the declaration is hoisted, not the initialization or assignment. In other words, if a variable is initialized after its declaration, its value will not be available until after the initialization. Similarly, if a function expression is used, it will not be hoisted, only function declarations are hoisted. JavaScript hoisting can have some advantages and disadvantages. Advantages: Flexibility : Hoisting provides developers with the flexibility to declare functions or variables anywhere in t