Data Types and Variables: Representation and Conversion

JavaScript handles data through dynamic typing. The runtime engine determines type automatically. This removes static compilation overhead but introduces type coercion risks. Robust systems require explicit understanding of how JavaScript stores, converts, and evaluates data.

Primitive Types and Number Literals

JavaScript operates exclusively with double-precision 64-bit floating-point format (IEEE 754) for all numeric values. There are no native integers.

This architectural decision creates specific data representation issues. Floating-point arithmetic cannot represent decimal fractions precisely. The evaluation of 0.1 + 0.2 results in 0.30000000000000004, not 0.3. Financial calculations and high-precision scientific operations must circumvent this hardware-level limitation using scaling logic or arbitrary-precision libraries.

Number Literals

Numeric values accept various representations.

  • Decimal Literals: Standard base-10 formatting.
  • Hexadecimal Literals: Prefixed with 0x or 0X. Commonly used for memory addresses, color codes, and bitwise operations.
  • Octal Literals: Prefixed with 0o or 0O. Legacy strict mode implementations ban implicitly defined octals (prefix 0) to prevent parsing errors.

Special Numeric Values

JavaScript handles mathematical limits and errors through predefined constants.

  • Infinity: Represents a value exceeding the upper limit of the floating-point numbers.
  • NaN (Not-a-Number): The result of an undefined or erroneous mathematical operation, such as dividing zero by zero. NaN is toxic; any mathematical operation involving NaN evaluates to NaN. Crucially, NaN is not strictly equal to itself (NaN === NaN is false). Diagnostics require the isNaN() or Number.isNaN() functions.

Type Conversion and the typeof Operator

Variables change types. JavaScript engine executes implicit type coercion during operations involving mismatched types.

  • Adding a string and a number concatenates the values: "5" + 2 yields "52".
  • Subtracting a string from a number forces numeric evaluation: "5" - 2 yields 3.

Implicit coercion introduces silent logic failures. Systems must rely on explicit type conversion functions like Number(), String(), and Boolean() to enforce data integrity.

The typeof Operator

The typeof operator interrogates an operand and returns a string indicating its type. It is the primary diagnostic mechanism for dynamic variables.

  • typeof 42 returns "number".
  • typeof "text" returns "string".
  • typeof true returns "boolean".
  • typeof undefined returns "undefined".

It has documented architectural flaws. typeof null returns "object". This is a legacy bug in the original implementation that cannot be patched without breaking backward compatibility.

Variables: Declaration and Scope

A variable maps an identifier to a memory location. Modern execution environments strictly enforce block scoping.

  • var: Function-scoped or globally-scoped. Vulnerable to hoisting logic errors. Variables declared with var are accessible before their declaration in the code block.
  • let: Block-scoped. Limits the variable to the immediate enclosing block {}. Prevents external mutation.
  • const: Block-scoped. Creates a read-only reference to a value. It does not freeze the value itself; if the const references an object, the object’s properties remain mutable. The binding itself cannot be reassigned.

Strict variable management prevents memory leaks and namespace collisions. const is the default. let is the fallback. var is obsolete.