2.4 Dynamic Typing

One thing that sets JavaScript apart from many other programming languages is that it is a dynamic language. In a static language, the type of a variable is fixed at declaration time and cannot change later. In a dynamic language like JavaScript, the type of a variable can change at runtime depending on the value you assign to it.

Inspecting types with typeof

JavaScript provides a special operator called typeof to inspect the type of a value. Let's reuse the name variable from the previous lesson: it was initialized to a string, so typeof name returns "string". If we reassign name = 1, typeof name now returns "number". The type literally changed at runtime. That's dynamic typing in action.

let name = 'Brahim';
console.log(typeof name); // "string"
name = 1;
console.log(typeof name); // "number"

Note that typeof is a reserved keyword, so you cannot use it as a variable name. In the browser console, you can clear previous logs with Ctrl + L to keep your tests readable.

The primitive and object types

  • number: there is no distinction between integers and decimals — 17 and 1.5 are both of type number.
  • boolean: true or false, for example a flag like isApproved.
  • undefined: when a variable is declared without a value, both its value and its type are undefined.
  • object: when we inspect a more complex value such as selectedColors, typeof returns "object".

JavaScript organizes types into two main categories: primitive types (number, string, boolean, undefined, null, symbol) and reference types (objects, arrays, functions). What is an object exactly? That's the topic of the next video.

Summary

This lesson introduces dynamic typing in JavaScript, explaining how variables can change their type at runtime—a key feature that distinguishes JavaScript from statically-typed languages. Through practical console examples using the typeof operator, the lesson demonstrates how the same variable can hold different types (strings, numbers, booleans, undefined) at different points in the code. It also clarifies that JavaScript treats all numbers uniformly as type 'number', without distinguishing between integers and floating-point values, and previews that objects will be covered in the next lesson.

Key points

  • Dynamic typing allows variable types to change at runtime based on assigned values, unlike static languages where types are fixed at declaration
  • The typeof operator is used to check a variable's current type (e.g., typeof name returns 'string' or 'number')
  • All numbers in JavaScript are of type 'number', whether they are integers or decimals—no separate type distinction
  • JavaScript recognizes primitive types (string, number, boolean, undefined) and reference types (objects)
  • undefined is both a type and a value—a variable that has been declared but not assigned has type 'undefined'
  • Variables can be reassigned to completely different types during program execution

FAQ

What is the difference between static and dynamic typing?

In static languages, a variable's type is fixed at declaration and cannot change. In dynamic languages like JavaScript, the variable's type is determined at runtime based on the value assigned to it and can change when the variable is reassigned a different type of value.

How do you check what type a variable holds in JavaScript?

You use the typeof operator followed by the variable name (e.g., typeof variableName). This can be executed in the browser console or in your code and will return the type as a string.

Why doesn't JavaScript distinguish between integers and decimals?

JavaScript treats all numeric values uniformly as type 'number'—it does not have separate types for whole numbers and floating-point numbers like some other languages do. Whether a number is 5 or 5.5, the typeof operator will return 'number'.