Loosely typed vs strongly typed languages

By

Static vs dynamic typing is not the same as strong vs weak typing. How JavaScript, Python, TypeScript and Java fit those labels, with examples.

~~~

JavaScript is often called a loosely typed language. People mix two different ideas when they say that.

One axis is static vs dynamic. Static typing means types are checked before the program runs, by a compiler or a type checker. Dynamic typing means types are checked at runtime.

The other axis is strong vs weak. A strongly typed language rarely converts values behind your back. A weakly typed one does a lot of implicit coercion. “Loosely typed” usually means weak, but people use it for dynamic too.

Those axes are independent. Dynamic does not mean weak. Static does not mean strong.

A quick map

LanguageStatic or dynamicStrong or weak (roughly)
JavaScriptDynamicWeak (lots of coercion)
PythonDynamicStrong
TypeScriptStatic checks, then JavaScript at runtimeSame as JavaScript once compiled
JavaStaticStrong

The labels get fuzzy at the edges, but the table is a good starting point.

Coercion in practice

JavaScript often converts types for you.

'5' + 1  // '51'  (string concatenation)
'5' - 1  // 4     (numeric subtraction)

Same digits, different operator, different result. That surprise is why people call JavaScript weakly typed.

Python refuses that mix:

'5' + 1
# TypeError: can only concatenate str (not "int") to str

You have to convert yourself, for example with int('5') + 1. Python is still dynamic. It just refuses to invent a number from a string.

TypeScript does not change any of this at runtime. It checks types while you write and build, then compiles to plain JavaScript, so the same coercion rules apply when the code runs.

Dynamic does not mean “no types” either. JavaScript has types. See my JavaScript Types post. You just use them without declaring them everywhere.

When it matters

On a solo script or a tiny experiment, dynamic typing feels great. You change things fast and keep moving.

On a team, or on a codebase you will refactor for years, static checks pay off. The compiler catches mismatched arguments and missing fields before users do. TypeScript is popular in that world for a reason: you keep the JavaScript platform, and you add structure where it helps most.

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about js: