INSTACK 360

JavaScript Data Types & Type Conversion

Explore JavaScript's dynamic typing system. Learn about primitives, objects, and how type coercion works.

Beginner 5 min read

Dynamic Typing

JavaScript is a loosely typed or dynamic language. 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.

let foo = 42;    // foo is now a number
foo = "bar"; // foo is now a string
foo = true;  // foo is now a boolean

Primitive Data Types

JavaScript has 7 primitive data types:

  • String: Text representation. e.g., "Hello"
  • Number: Both integer and floating-point. e.g., 42 or 3.14
  • BigInt: For numbers extremely large.
  • Boolean: true or false
  • Undefined: A variable that has not been assigned a value.
  • Null: Intentional absence of any object value.
  • Symbol: A unique and immutable primitive value.

Type Coercion vs Conversion

JavaScript often automatically converts types behind the scenes (Coercion). To strictly convert types, use explicit functions.

// Implicit Coercion
console.log('5' + 1); // "51" (String concatenation)
console.log('5' - 1); // 4 (Numeric subtraction)

// Explicit Conversion
let x = Number("5"); // 5