What Does Interpreted, Dynamic, and Weakly Typed Mean?
If you were anything like me getting started, all of that meant nothing. Let’s break this down a bit.
What are Types?
A type is a way of defining the qualities of data. It let’s us know what possible values the data can have, and what kinds of operations we can expect to do with that data. For instance, when we know the data is the type “boolean” we know that it’s values can only ever be “true” or “false”.
Interpretation vs Compilation
C and Go are compiled languages. Compilation is the process of taking a “high-level” language and generally turning it into a stand alone executable. After compilation the executable can be run without the compiler again and again. Data is provided to executable and the compiled program returns the output.
Interpreted languages like JS are executed using an interpreter. The interpreter reads the source code with data, and produces the output. Interpreted languages cannot be run without the use of an interpreter. However, because the code and data are interpreted in “real time” there is a lot more flexibility than with compilation.
Compilation has the benefit of improved performance, because much of the analysis is done ahead of time. Interpretation has to do it’s analysis as the program is executing.
Dynamic vs Static Type Checking
Dynamic and static refer to when data types are checked. In static typing, types are checked “statically”. This means that the source code is analyzed and checked for types before it is ever executed. The most common implementations of static type checking occur during compilation. It is a common misconception that static typing explicitly means that types are checked at compilation. The two are independent.
Static type checking has the benefit of letting the coder know ahead of time if there are type errors in their code. Originally, static type checking was developed to prevent coders from implementing “unsafe” code that could cause the program to run improperly. This was important in the early days of computing when computation was expensive and limited.
Dynamic type checking occurs at runtime. This means that as the code is running analysis is performed on the data to determine its type.
How strong or weak the language’s typing is determines how the type checking handles the data types.
Weak vs Strong Typing
Strong and weak are a bit more subjective. Generally speaking, a “strongly” typed language is more consistent, predictable, and explicit with the way it handles types, and a “weakly” typed language could be seen as less predictable, but more flexible and expressive. Go is considered a strongly typed language and JS a weakly typed language.
In our go example, we declare variables ‘a’ and ‘b’ as an int and string accordingly. When we try to add ‘a’ and ‘b’ together, we will get an error that their types don’t match, and Go just can’t even. The above example will not compile, because of the type error.
In JS when we try to add together the number 6 and the string “5”, it returns the string “65”. In another weakly typed language the language might return the number 11.
JS sees that you are trying to add a string to a number, it coerces the number 6 into a string, and concatenates the two string giving you “65”. Our other language did the opposite. It tries to coerce the string “5” into the number 5, adds 6 and 5 together and gives you the number 11.
These are examples of “weak” type handling. The language tries to work with what you give it. In “strong” type handling the language will be more agressive about enforcing types. “You want to add a string to a number? Too bad.”
That’s it Folks
That is all I have for today. You should walk away from this with a better understanding of how JS works, and how that compares to other kinds of programming languages.
Up next, we will be discussing all the available types in JS. YAY TYPES!!!
Until next time…