Typing in Programming Languages

Typing in Programming Languages

·

3 min read

In programming languages, we can best describe type as a classification of information. We often seen this in our numbers, strings, booleans etc. Different programming languages deal with type in different ways.

Weak vs. Strong Typing

A big reason that Javascript is a great first programming language to learn is that it is very forgiving. It does not ask for you to meet strict rules, giving the programmer multiple options to obtain the same result. This can be explained by its use of what is called weak typing.

What is Weak Typing?

A programming language can be described as having weak typing when it does not require you to explicitly define type. Languages like JavaScript fit into this category because they don't mind doing some guesswork to figure out what type you intend to set. While other languages are stricter and may ask you to assign type upon setting a variable, languages with weak typing bend the rules for us, letting you work around the type system.

We can see JavaScript's 'guesswork' in action by using the typeof operator:

typeof 42
//=> "number"

typeof 'blubber'
//=> "string"

typeof false
//=> "boolean

JavaScript will even take the liberty of changing type for you in instances where it thinks it makes sense, however, this also means that sometimes JavaScript's attempts may be a tad too ambitious...

'5' + 6
//=> '56'

10 - true 
//=> 9

5 + foo 
//=> 7

"bar" + {} 
//=> "bar[object Object]

In short, weakly typed languages give the programmer freedom to write code without worrying about strict typing rules, but this can come yield strange results that the programmer may not be prepared for.

What is Strong Typing?

To preface, strong typing does not have a universally agreed-upon technical meaning. Regardless, this section will broadly illustrate what is often referred to as strong typing.

The most widely used description for a strongly typed programming language is that the rules of the language's type system are too strict to allow the programmer to work around them.

In contrast to JavaScript, programming languages like Ruby are considered to be strongly typed ( though some would argue that calling it stronger-typed is more accurate). In these languages, there is no effort on the language's end to do type conversion for the user. If we attempted to run any of the above code in Ruby, we would be met with a type error. In many cases, this is preferable to the awkward return values that we can get from JavaScript.

Static Typing vs. Dynamic Typing

Arguably simpler in concept than strong and weak typing, the definitions for static and dynamic typing are more universally agreed upon. While weak and strong typing have to do with how strict a language's type checking is, static and dynamic typing describe when this checking takes place.

First, let's briefly review the process that occurs when written programs are run. When running a program, the written code needs to be converted into a form that the computer can understand and run. This process of conversion is called compilation, and the period of time in which this occurs is called compile-time. The period of time in which the program runs is called runtime.

What is Static Typing?

Languages with static typing will check for type errors during compilation time:

  1. Check Types
  2. Run

In this case, the program will not even run if a type error is found. Common languages that use static typing are C#, C++, and Java.

What is Dynamic Typing?

Languages with dynamic typing will check for type errors during runtime:

  1. Run
  2. Check Types

In this case, the program will run and only thow an error when that particular code line is executed. Common languages that use dynamic typing are JavaScript, Ruby, and PHP.

Additional Resources

For more information on typing, refer to the following: