Back to series
· 8 min read

Strongly Typed vs. Weakly Typed Languages


I. What Are Strongly Typed Languages and Weakly Typed Languages?

Strongly typed languages and weakly typed languages are mainly used to describe how strictly a language constrains operations between values of different types, as well as how strict its type conversion rules are.

  • Strongly typed language: Generally imposes stricter constraints on operations between different types and does not allow incompatible type operations at will.
  • Weakly typed language: Generally allows more implicit type conversions, and some values of different types can directly participate in operations or comparisons.

For example:

Python:

age = 18
name = "Tom"

print(age + name)

Output:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python does not allow an integer and a string to be added directly.

JavaScript:

let age = 18;
let name = "Tom";

console.log(age + name);

Output:

18Tom

JavaScript converts the number to a string and then performs string concatenation.

These two examples illustrate the differences between languages in type conversion and operation rules.

Pay special attention to the following:

  • Strong typing does not equal static typing.
  • Weak typing does not equal dynamic typing.
  • Strong typing does not mean type conversion is completely prohibited.
  • Weak typing does not mean there is no type system.

Strong and weak typing are not a strict classification with a unified mathematical definition. Depending on the language features and the context of type theory, the use of these two concepts may vary.

II. Before Understanding Strong and Weak Typing, You Need to Master Type Conversion

The difference between strong and weak typing is often reflected in type conversion rules.

1. What Is Type Conversion?

Type conversion refers to converting a value of one data type into a value of another data type.

For example:

age = 18

text = str(age)
print(text)
print(type(text))

Output:

18
<class 'str'>

Here, str() is used to convert an integer to a string.

Type conversion is generally divided into two categories:

  • Implicit Conversion: Automatically completed by the language runtime or compiler according to rules.
  • Explicit Conversion: The conversion operation is explicitly specified by the developer.

2. Explicit Type Conversion

Explicit type conversion requires developers to actively express the intent to convert.

Python:

age = 18

text = str(age)
number = int("100")

print(text)
print(number)

Java:

int number = 100;

double price = (double) number;
int value = (int) price;

The advantage of explicit conversion is that the conversion intent is clear and the code is easier to understand.

But explicit conversion is not necessarily safe. For example, converting a floating-point number to an integer may lose the fractional part.

double price = 19.99;
int value = (int) price;

System.out.println(value); // 19

3. Implicit Type Conversion

Implicit type conversion means that the program does not explicitly call a conversion function, but the language automatically completes the type conversion according to its own rules.

For example, in Java:

int number = 10;
double result = number;

System.out.println(result);

Output:

10.0

The integer is automatically converted to a floating-point number.

JavaScript:

console.log("10" + 5);
console.log("10" - 5);
console.log("10" * 5);

Output:

105
5
50

Different operators trigger different type conversion rules:

  • +: When a string is involved in an operation, string concatenation may be performed.
  • -: Usually attempts to convert the operands to numbers.
  • *: Usually attempts to convert the operands to numbers.

This is also where JavaScript's type conversion rules can easily cause problems.

4. Basic Process of Type Conversion

flowchart TB
    A["Data involved in the operation"] --> B["Check operand types"]
    B --> C{"Whether operation rules are satisfied"}
    C -->|"Satisfied"| D["Execute the operation directly"]
    C -->|"Not satisfied"| E{"Whether automatic conversion is allowed"}
    E -->|"Allowed"| F["Perform implicit type conversion"]
    E -->|"Not allowed"| G["Report a type error"]
    F --> H["Execute the operation"]
    D --> I["Obtain the result"]
    H --> I

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note that real-world languages do not necessarily follow this unified process. Different languages have their own rules for operator overloading, type promotion, object protocols, and conversion precedence.

III. What Is a Strongly Typed Language?

1. Core Characteristics of Strong Typing

Strongly typed languages usually impose relatively strict restrictions on operations between different types.

For example, Python:

number = 10
text = "20"

result = number + text

At runtime, a TypeError is thrown.

If a developer wants to concatenate strings, an explicit conversion is required:

number = 10
text = "20"

result = str(number) + text

print(result)

Output:

1020

Strongly typed languages emphasize type constraints and prevent data of different types from being arbitrarily mixed in operations without clear rules.

2. Typical Characteristics of Strongly Typed Languages

Clear type constraints

Operations between different types must satisfy the type rules specified by the language.

Fewer accidental conversions

Unrelated data types are usually not converted to other types arbitrarily.

Errors are easier to locate

Incompatible type operations are rejected by the type system or produce a clear exception at runtime.

Clear interface constraints

Function parameters, return values, and object operations usually have clear type requirements.

Easier to maintain

Type constraints can help developers understand data structures and business interfaces, reducing some hidden errors.

3. Common Strongly Typed Languages

Language Type characteristics
Python Dynamic typing, relatively strict type constraints
Java Static typing, with clear type conversion rules
Rust Static typing, strict type constraints
Go Static typing, does not allow arbitrary mixing of incompatible types
C# Static typing, with clear type conversion rules
Ruby Dynamic typing, generally does not allow incompatible objects to participate directly in operations

Note that strongly typed languages do not completely prohibit implicit conversion. For example, Java allows automatic promotion of some numeric types:

int number = 10;
double price = number;

This is a valid implicit type conversion.

Therefore, the focus of strong typing is not whether implicit conversion exists, but how the language constrains compatibility relationships between types, and whether unsafe or unreasonable operations are allowed.

IV. What Is a Weakly Typed Language?

1. Core Characteristics of Weak Typing

Weakly typed languages usually allow more implicit conversions between types.

For example, JavaScript:

let a = "10";
let b = 5;

console.log(a + b);
console.log(a - b);

Output:

105
5

The string "10" and the number 5 can participate in the operation.

JavaScript automatically performs the corresponding conversion based on the operator and the operand types.

2. Typical Characteristics of Weakly Typed Languages

More implicit conversions

Data of different types may be automatically converted before participating in operations.

Flexible to use

Developers can accomplish operations with less explicit conversion code.

Results depend on conversion rules

The same data may produce different results when different operators are used.

Prone to hidden errors

Implicit conversion can mask data type problems, causing program output to differ from what the developer expects.

Edge cases require attention

Conversions among null, undefined, boolean, string, and number values can lead to complex behavior.

3. Typical Problems in JavaScript

JavaScript's implicit conversion mechanisms are extensive.

For example:

console.log("5" + 2);
console.log("5" - 2);
console.log("5" * 2);
console.log("5" / 2);

Output:

52
3
10
2.5

Here are a few more expressions that are easy to misinterpret:

console.log([] + []);
console.log([] + {});
console.log("0" == false);
console.log(null == undefined);

Output:


[object Object]
true
true

These results are related to JavaScript's object conversion and abstract equality comparison rules.

They are not random behavior; they are results defined by the language specification.

In real-world development, understanding these rules is more important than simply memorizing a particular result.

V. The Core Differences Between Strong and Weak Typing

Comparison dimension Strong typing tendency Weak typing tendency
Type constraints Relatively strict Relatively loose
Implicit conversion Usually subject to more restrictions Usually permits more conversions
Incompatible operations More likely to be rejected May be automatically converted and then executed
Type errors More likely to expose unreasonable operations Some errors may be masked by conversion rules
Code expression Type conversion intent is relatively explicit Some operations can be expressed more concisely
Maintenance risk Helps reduce some type errors Requires closer attention to conversion details

It should be emphasized that this table describes general tendencies, not strict criteria for classifying languages.

There is no unified quantitative standard for strong and weak typing that applies to all languages. When judging a specific language, one should analyze its type system and conversion rules.

VI. Strong Typing Is Not Static Typing, and Weak Typing Is Not Dynamic Typing

This is one of the most easily confused points in learning programming languages.

They belong to two different dimensions of classification.

Classification Question of concern
Static typing vs. dynamic typing When types are determined and checked
Strong typing vs. weak typing How operations and conversions between types are constrained

Simply put:

  • Static vs. dynamic mainly concerns when type checking occurs.
  • Strong vs. weak mainly concerns type constraints and conversion rules.

1. Combinations of the Four Concepts

This can be understood along two dimensions:

Language Type checking method Tendency of type constraints
Python Dynamic typing Strong typing
Java Static typing Strong typing
JavaScript Dynamic typing Generally described as weak typing
C Static typing Has many implicit conversions and low-level type operation capabilities

Among these, the classification of C deserves particular caution.

C has a static type system, but it supports integer promotion, arithmetic type conversion, pointer conversion, and many other rules, and it also allows some type constraints to be bypassed through pointers and low-level memory operations. As a result, it is sometimes described as relatively weakly typed, though assessments vary across contexts.

2. Why Is Python a Dynamically and Strongly Typed Language?

Python is a typical example.

value = 100
value = "hello"

This code runs fine.

Because Python is a dynamically typed language, a variable name can be bound to objects of different types at runtime.

However:

result = 100 + "hello"

throws a TypeError.

This shows that:

  • Python's types are determined at runtime.
  • A variable name can be rebound to objects of different types.
  • Objects themselves have definite types.
  • Incompatible type operations are usually not automatically converted and then executed.

So Python can possess the characteristics of both dynamic typing and strong typing at the same time.

3. Why Is JavaScript Often Called a Dynamically and Weakly Typed Language?

JavaScript also uses dynamic typing.

let value = 100;
value = "hello";

A variable can be rebound to a value of a different type.

At the same time, JavaScript allows many implicit conversions:

console.log("100" - 1);
console.log("100" + 1);

Output:

99
1001

For this reason, JavaScript is usually classified as a dynamically and weakly typed language.

However, this classification is a generalization of its type conversion behavior; it does not mean that JavaScript has no type constraints.

VII. JavaScript's Implicit Type Conversion Mechanism

JavaScript is an important case for understanding weak typing characteristics.

Its implicit type conversion is not simply a matter of uniformly converting all data into strings or numbers; instead, it executes different conversion algorithms depending on the operation.

1. String Conversion

String conversion is usually performed through String() or the language's internal conversion rules.

console.log(String(100));
console.log(String(true));
console.log(String(null));
console.log(String(undefined));

Output:

100
true
null
undefined

2. Number Conversion

JavaScript can use Number() to convert data of different types into numbers.

console.log(Number("100"));
console.log(Number("10.5"));
console.log(Number(true));
console.log(Number(false));
console.log(Number(null));
console.log(Number(undefined));

Output:

100
10.5
1
0
0
NaN

A few results that are easy to confuse:

  • Number(true) yields 1.
  • Number(false) yields 0.
  • Number(null) yields 0.
  • Number(undefined) yields NaN.
  • Number("hello") yields NaN.

NaN means that the result is not a valid number (Not-a-Number).

3. Boolean Conversion

In JavaScript, Boolean() can be used to convert a value into a boolean.

console.log(Boolean(0));
console.log(Boolean(1));
console.log(Boolean(""));
console.log(Boolean("hello"));
console.log(Boolean(null));
console.log(Boolean([]));

Output:

false
true
false
true
false
true

Special attention is needed here:

The empty array [] and the empty object {} are both truthy values in JavaScript.

This differs from the behavior of languages such as Python.

Python:

print(bool([]))
print(bool({}))

Output:

False
False

4. Implicit Conversion in Equality Comparison

JavaScript provides two common forms of equality comparison:

  • ==: loose equality comparison, which may perform type conversion.
  • ===: strict equality comparison, which will not judge values of different types as equal due to loose type conversion.

For example: