From "You Don't Know JS: Types & Grammar"
🎧 Listen to Summary
Free 10-min PreviewJavaScript Data Types and Variables
Key Insight
JavaScript values, the fundamental units of information for program state, exist as either primitive or object types. Primitive values are embedded using literals; strings use single, double, or back-tick quotes, with back-ticks enabling variable interpolation, e.g., '`My name is ${ firstName }.`'. Other primitives include booleans ('true', 'false') and numbers, which can be integers, decimals like 3.141592, or arbitrarily large 'bigint's, often used for counting or 0-based array indexing like 'names[1]'. Special primitive values 'null' and 'undefined' both signify absence, with 'undefined' generally preferred for consistency, while 'symbol' provides hidden, unguessable keys on objects, for example 'hitchhikersGuide[ Symbol("meaning of life") ]; // 42'.
Object values, distinct from primitives, include arrays and general objects. Arrays are ordered, numerically indexed lists, capable of holding any value type, like 'var names = [ "Frank", "Kyle", "Peter" ]; names[0]; // Frank'. Objects are unordered, keyed collections, where values are accessed by string keys (properties), for instance 'var me = { first: "Kyle" }; me.first;'. The 'typeof' operator determines a value's built-in type for primitives (e.g., 'number', 'string', 'boolean', 'undefined', 'function') or 'object' otherwise, although it incorrectly identifies 'null' as 'object' and does not return 'array' for arrays.
Values are stored in variables, which must be declared. 'var' declares function-scoped variables, allowing wider access, e.g., 'var myName = "Kyle";'. 'let' introduces block-scoped variables, limiting their accessibility to the enclosing block, as seen when 'let age = 39;' inside an 'if' block prevents access outside. 'const' declares block-scoped variables that must be initialized and cannot be re-assigned, though object values assigned to 'const' can still be internally mutated (e.g., 'const actors = [ "Morgan Freeman" ]; actors[1] = "Jennifer Aniston"; // OK'). Other declarations, like function parameters and 'catch' clause variables, also define identifiers with specific scoping behaviors.
📚 Continue Your Learning Journey — No Payment Required
Access the complete You Don't Know JS: Types & Grammar summary with audio narration, key takeaways, and actionable insights from Kyle Simpson.