Strong Typing and Basic Types
Discover the distinction between strong typing in JavaScript and TypeScript and how TypeScript enforces rules for accurately using variables, functions, and objects.
We'll cover the following...
JavaScript is not strongly typed. It is a language that is very dynamic, as it allows objects to change their types, properties, and behavior on the fly. TypeScript, however, is strongly typed and, as such, will enforce rules that govern how we use variables, functions, and objects.
We will start with the concepts of strong typing or static typing, as it is also known. We will then move on to exploring some of the basic types that the language uses and how the language can detect what type a variable is based on how and where it is used within our code. We will then tackle the basics of function signatures and finally see how to import and use third-party JavaScript libraries.
Strong typing
Strong typing means that when we create a variable or define a parameter in a function, we specify what type it is. Once this type has been specified, we cannot change it. Loose typing is the exact opposite of this concept, and JavaScript is a loosely typed language.
As an example of loose typing in JavaScript, let’s take a look at the following code:
// Declare a variable called "test" and initialize it with a string valueconsole.log("Let's declare the variable 'test' with a string value\n");var test = "a string";// Log the value of "test" to the consoleconsole.log("test = " + test);console.log("\nWe can see the successful assignment of a string type to the 'test' variable.\n");// Change the value of "test" to an integerconsole.log("Now let's assign an integer value to 'test'\n");test = 1;console.log("test = " + test);console.log("\nWe can see the successful assignment of an integer type to the 'test' variable. Notice that the type of the 'test' variable has changed from a string to an integer.\n");// Change the value of "test" to a functionconsole.log("Now let's assign a function to 'test'\n");test = function (a, b) {return a + b;}console.log("test = " + test + "\n");console.log("This is how types work in JavaScript: variables can hold values of any data type, and the type of a variable can change during the execution of a program. This is in contrast to strongly-typed languages like TypeScript, in which variables must be explicitly declared with a specific data type and can only hold values of that type.");
-
On line 3 of this code snippet, we declare a local variable named
test
and assign ita string
value. We then use theconsole.log
function to print its value to the console. We display a message indicating that the assignment of a string value totest
was successful. -
On line 12, we assign an integer value of
1
to thetest
variable and print its value to the console. We display a message indicating that the assignment of an integer value totest
was successful. -
Finally, from line 24 — 26, we assign a function that takes two parameters, named
a
...