Static vs Dynamic Type Checking
Introduction to Static and Dynamic type checking in JavaScript.
Introduction
JavaScript makes declaring types convenient. The type coercion feature saves us from thinking up types for variables. In fact, sometimes it makes us wonder; how does JavaScript manage to do all this? Also, is this only a JavaScript feature?
To answer that we need to understand two common techniques:
- Static type checking
- Dynamic type checking
These two techniques are widely used in all languages to define types for each variable when the difference between techniques lies in the timing.
So, let’s take a look at the two.
Static type checking
The most generic way of defining types, static type checking occurs before the code is run. C++, Go, and other languages use this technique for checking types.
NOTE: Because JavaScript cannot use this technique, TypeScript or Flow are used instead.
Dynamic type checking
Dynamic type checking is the process where type is checked and assigned during run time. This happens on the fly. Languages that use this technique are interpreted or use a JIT compiler. JavaScript uses this technique.
With dynamic type checking, we can use type coercion seamlessly.
JavaScript type checking
Check out the code snippet below.
Get hands-on with 1400+ tech skills courses.