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: 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 and select Playground: You will … Continue reading Introduction to TypeScript

JavaScript – Calling Functions

Let’s go through an example of different ways to call a function in JavaScript. Let’s say we have a function that prints Hello World to the console. We can call this as: function HelloWorld() { console.log(“Hello World”); } HelloWorld(); This function can also be assigned an anonymous variable and called as below: var i = function() { console.log(“Hello World”); } i(); Or also run immediately as: var i = function() … Continue reading JavaScript – Calling Functions

JavaScript – Use Strict

“use strict” is a feature of JavaScript that tells the compiler to use a strict context. For example, consider the code below. This code produces no errors, and displays “1” in the console: However, let’s say we didn’t include “var” before our variable. In normal JavaScript without using “use strict”, this would be fine. However, with “use strict”, it throws an error: For more information: