Static vs Dynamic Typing

Every programming language employs a type system in order to assign a type to each variable declared in the code.   These type systems can be divided into two categories: static and dynamic.

Statically-typed languages require you to declare the data types of your variables before you use them.  A variable has its type determined at compile-time and that type does not change throughout the lifespan of the variable.  Examples of statically-typed languages include Java and C#.  

// Static typing in Java
int num = 100;
char a = 'a'; 

In contrast, dynamically-typed languages allow a variable to have different types through its lifetime.

Dynamic typing can be quite powerful, but it also lets you shoot yourself in the foot without even noticing it.  For this reason, it's important to understand how the underlying engine makes type decisions.   Examples of  dynamically-typed languages include JavaScript, PHP, and Python.  

Dynamic typing can lead to coercion of values into other types during certain operations such as comparison.

// Javascript
var a = 1, b = '2', c = 0, d = false;

// Outputs 12 because a is coerced to string '1'
console.log(a + b);

// Outputs true because d is coerced to number 0 
console.log(c == d);

// Outputs false because the c & d are not the same type
console.log(c === d);

When checking for equality, stick with the strict equality operator (===) so the interpreter will compare both the value and the type.