Function Overloading in JavaScript

Function Overloading is the ability to define two or more functions with the same name that have a different signature, i.e. a number and/or different types of parameters/arguments. For example: function DoSomething(a) function DoSomething(a,b) function DoSomething(a,b,c) In JavaScript, there is no function overloading. There is however behavior regarding sending different parameters, and there is a pattern we can use to handle function overloading. First, consider the case above. If we … Continue reading Function Overloading in JavaScript

JavaScript Promises

JavaScript promises are a way in JavaScript to write asynchronous code in a synchronous way. They are asynchronous callbacks, but in terms of error handling are much more readable. Here we will go through an example of how to use promises. We define a promise as below, with 2 parameters, resolve and reject: new Promise((resolve, reject) If the promise is resolved, we can perform an action, and if it is rejected, … Continue reading JavaScript Promises

JavaScript Callback Functions

A callback function in JavaScript is a function passed to another function that is then called after an event has occurred. This is useful when you don’t know how much time the function will take to complete, such as a call to a web API. In the simple example below, we have a function called callback() that writes to the console. We then have a function called testCallback() that calls the … Continue reading JavaScript Callback Functions

Namespaces in JavaScript

Namespaces are used to help organize code into logical groups. An example of a namespace is MyCo.ERP.Customers, which may contain code regarding ERP Customers. I could also have a namespace called MyCo.CRM.Customers. When writing an app, I can reference each piece of code using their fully qualified name to avoid confusion of what methods I would like to run. In languages like C#, namespaces are easily implemented. In JavaScript, we can … Continue reading Namespaces in JavaScript

JavaScript === (Triple Equals)

The triple equals sign in JavaScript means “equality without type coersion”. That means, the type and values must both be equal. Take for example the scenario where 0 is false. If we compare with 2 equal signs, the following statement is true. This is because the equality operator converts if they are not of the same type: 0 == false // true If we compare the same 0 and false … Continue reading JavaScript === (Triple Equals)

JavaScript Error Handling

JavaScript contains different ways to handle errors. One way is using try/catch/finally. In the example below, variable y does not exist. try { var x = 1 + y; console.log(t); } catch(e) { console.log(“Error: ” + e); } finally { console.log(“Run finally”); } Running this produces: Note you can also use name, message and stack to get error information: try { var x = 1 + y; console.log(t); } catch(e) … Continue reading JavaScript Error Handling

Debugging TypeScript with Google Chrome and Node

To debug TypeScript using Visual Studio Code, install the Debugger for Chrome, located at: https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome: You will see the message: The debugger will now be installed: Let’s say in our environment we have: A folder called TS with TypeScript files A TS file called HelloWorld.ts Like below: And we have an index.html page which calls the HelloWorld.ts file: Now, update your tsconfig.json file like below: And the launch.json file: Now, if … Continue reading Debugging TypeScript with Google Chrome and Node

Installing TypeScript for Visual Studio Code

Visual Studio Code has support for the TypeScript language. To use it, create a new file with TypeScript code in Visual Studio Code: And save the file, we will see TypeScript as an option: We can see Visual Studio Code has TypeScript Intellisense: Visual Studio Code does not include the TypeScript Compiler (tsc). To install it using the Visual Studio Code terminal, type: npm install -g typescript You will see … Continue reading Installing TypeScript for Visual Studio Code

Installing TypeScript on Node

To install TypeScript on Node, run the command: npm install -g typescript Now let’s create a new TypeScript file. First create a directory for our TypeScript project called TS, and create a new file HelloWorld.ts: Run the TypeScript compiler command: tsc HelloWorld.ts This will create a new js file: With JavaScript code:  

Introduction to TypeScript

TypeScript is a superset of JavaScript developed by Microsoft that supports classes, modules and interfaces, along with several other features. It is a static type language, meaning it will validate code at development time. JavaScipt is a dynamic language, which does not validate code at development time. Having a static type language is very useful for building robust code. To try out TypeScript online, go to www.typescriptlang.org and select Playground: You will … Continue reading Introduction to TypeScript