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
0xor0X. Commonly used for memory addresses, color codes, and bitwise operations. - Octal Literals: Prefixed with
0oor0O. Legacy strict mode implementations ban implicitly defined octals (prefix0) 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.
NaNis toxic; any mathematical operation involvingNaNevaluates toNaN. Crucially,NaNis not strictly equal to itself (NaN === NaNis false). Diagnostics require theisNaN()orNumber.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" + 2yields"52". - Subtracting a string from a number forces numeric evaluation:
"5" - 2yields3.
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 42returns"number".typeof "text"returns"string".typeof truereturns"boolean".typeof undefinedreturns"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 withvarare 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 theconstreferences 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.