Programming Search⌘K

Featureintro

Dynamic and weak typing

Also called dynamic typing, weak typing, dynamically typed, weakly typed, implicit conversion.

In JavaScript, values have types and variables do not, so a variable can hold any type. Operators convert mismatched operands to a type they accept, in place of a type error.

Language
JavaScript

Synopsis

let foo = 42;
console.log(typeof foo);
foo = "bar";
console.log(typeof foo);
foo = true;
console.log(typeof foo);

const n = 42;
console.log(n + "1", typeof (n + "1"));
Output
number
string
boolean
421 string

"JavaScript is a dynamic language with dynamic types." "JavaScript is also a weakly typed language, which means it allows implicit type conversion when an operation involves mismatched types, instead of throwing type errors."1

Semantics

r1. Dynamic typing. "Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types".1 MDN defines dynamically typed languages as those "where the interpreter assigns variables a type at runtime based on the variable's value at the time."2 Example: synopsis.

r2. Against static typing. In a statically typed language, such as Java, C or C++, "variable types are known at compile time."3 JavaScript has no type declarations on variables, so a check such as typeof runs when the program runs. Example: guard.

r3. Weak typing. An operator with mismatched operands converts them. MDN calls this type coercion: "the automatic or implicit conversion of values from one data type to another (such as strings to numbers)."4 Example: coercion.

r4. The direction of a conversion. In '5' + 9, "JavaScript has coerced the 9 from a number into a string", which gives '59'. To get 14, "you'd have to explicitly convert the 5 to a number using the Number() method".4 Other arithmetic operators convert to numbers, so '5' * 2 is 10. Example: coercion.

r5. Bugs from coercion. "Implicit coercions are very convenient, but can create subtle bugs when conversions happen where they are not expected, or where they are expected to happen in the other direction".1

r6. Limits. "For symbols and BigInts , JavaScript has intentionally disallowed certain implicit type conversions."1 "A TypeError is thrown if BigInt values are mixed with regular numbers in arithmetic expressions".1 For a Symbol, Symbol('foo') + 'bar' "throws a TypeError".5 Example: bigint-mix and symbol-concat.

Examples

The examples run with Node.js 24.

Coercion in each direction, and an explicit conversion (rules r3 and r4):

console.log("5" + 9, "5" * 2, "5" - 2, Number("5") + 9);
console.log("foo" + 1, true + 1, [1, 2] + "", null + 1);
Output
59 10 3 14
foo1 2 1,2 1

A BigInt and a Number in one arithmetic expression throw (rule r6):

This code raises TypeError, on purpose.

console.log(1n + 1);

A Symbol does not convert to a string by + (rule r6):

This code raises TypeError, on purpose.

console.log(Symbol("foo") + "bar");

The type check runs at run time. A function that needs a number tests its argument (rule r2):

function double(x) {
  if (typeof x !== "number") {
    throw new TypeError(`expected a number, got ${typeof x}`);
  }
  return x * 2;
}
console.log(double(21));
try {
  double("21");
} catch (e) {
  console.log(e.message);
}
Output
42
expected a number, got string

Pitfalls

+ with a string

If one operand of + is a string, + joins strings, so '5' + 9 is '59', not 14. Convert with Number() first (rule r4).4

One variable, many types

A variable can change type at any assignment (rule r1). Code that reads the variable later can get a type that it does not expect. Test the type where it matters, as in guard.

Comparison

Topic JavaScript Python
where the type is on the value1 "Every object has an identity, a type and a value."6
can the type change a variable can take any type1 "an object’s type is also unchangeable"6
+ with a string and a number converts the number to a string4 the operands "must either both be numbers or both be sequences of the same type"7

Both languages put the type on the value. Python does not convert a number to a string for +, so '5' + 9 raises a TypeError in Python, but gives '59' in JavaScript.7 4 See Objects, values and types.

References

  1. JavaScript data types and data structures, MDN, Dynamic and weak typing; BigInt type; Type coercion
  2. Dynamic typing, MDN Glossary, introduction
  3. Static typing, MDN Glossary, introduction
  4. Type coercion, MDN Glossary, introduction; Examples
  5. Symbol, MDN, Symbol type conversions
  6. Data model, The Python Language Reference, 3.1 Objects, values and types
  7. Expressions, The Python Language Reference, 6.7 Binary arithmetic operations

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source