JavaScript Language Reference
JavaScript Language Reference
Section titled “JavaScript Language Reference”This comprehensive reference covers the complete JavaScript language specification, from basic syntax to advanced features.
Core Language Features
Section titled “Core Language Features”Lexical Structure
Section titled “Lexical Structure”Data Types and Values
Section titled “Data Types and Values”- Primitive Types
- Number, String, Boolean, Symbol, BigInt
- null and undefined
- Objects and References
- Type Conversion
Variables and Scope
Section titled “Variables and Scope”Functions
Section titled “Functions”Classes and Objects
Section titled “Classes and Objects”Control Flow
Section titled “Control Flow”Modules
Section titled “Modules”Asynchronous JavaScript
Section titled “Asynchronous JavaScript”ECMAScript Versions
Section titled “ECMAScript Versions”ES2024 (ES15)
Section titled “ES2024 (ES15)”- Array Grouping
- Promise.withResolvers()
- Regular Expression v flag
- Atomics.waitAsync()
ES2023 (ES14)
Section titled “ES2023 (ES14)”- Array.findLast() and findLastIndex()
- Hashbang Grammar
- Symbols as WeakMap keys
- Change Array by copy methods
ES2022 (ES13)
Section titled “ES2022 (ES13)”- Top-level await
- Private class fields
- Static class fields
- RegExp match indices
- Array.at() method
ES2021 (ES12)
Section titled “ES2021 (ES12)”- String.replaceAll()
- Promise.any()
- Logical assignment operators
- Numeric separators
- WeakRefs and FinalizationRegistry
ES2020 (ES11)
Section titled “ES2020 (ES11)”- BigInt primitive type
- Optional chaining (?.)
- Nullish coalescing (??)
- Dynamic imports
- globalThis object
ES2019 (ES10)
Section titled “ES2019 (ES10)”- Array.flat() and flatMap()
- Object.fromEntries()
- String.trimStart() and trimEnd()
- Optional catch binding
- Symbol.description
ES2018 (ES9)
Section titled “ES2018 (ES9)”- Async iteration
- Rest/spread for objects
- Promise.finally()
- RegExp improvements
- Template literal revision
ES2017 (ES8)
Section titled “ES2017 (ES8)”- Async functions
- Shared memory and atomics
- Object.values() and entries()
- String padding methods
- Object.getOwnPropertyDescriptors()
ES2016 (ES7)
Section titled “ES2016 (ES7)”- Exponentiation operator (**)
- Array.includes()
ES2015 (ES6)
Section titled “ES2015 (ES6)”- Classes
- Modules
- Arrow functions
- Template literals
- Destructuring
- Default parameters
- Rest and spread
- let and const
- Promises
- Maps and Sets
- Symbols
- Iterators and generators
Advanced Topics
Section titled “Advanced Topics”Memory Management
Section titled “Memory Management”// Understanding garbage collectionlet obj = { data: new Array(1000000) };obj = null; // Makes object eligible for GC
// WeakMap for memory-conscious cachingconst cache = new WeakMap();function expensiveOperation(obj) { if (cache.has(obj)) { return cache.get(obj); } const result = /* expensive computation */; cache.set(obj, result); return result;}
Performance Optimization
Section titled “Performance Optimization”// Avoid creating functions in loops// Badfor (let i = 0; i < items.length; i++) { items[i].addEventListener('click', function() { console.log(i); });}
// Goodfunction handleClick(index) { return function() { console.log(index); };}for (let i = 0; i < items.length; i++) { items[i].addEventListener('click', handleClick(i));}
Metaprogramming
Section titled “Metaprogramming”// Proxies for advanced object behaviorconst target = {};const proxy = new Proxy(target, { get(obj, prop) { return prop in obj ? obj[prop] : `Property ${prop} doesn't exist`; }, set(obj, prop, value) { if (typeof value === 'string') { obj[prop] = value.toUpperCase(); } else { obj[prop] = value; } return true; }});
// Reflect for meta operationsconst obj = { a: 1 };console.log(Reflect.has(obj, 'a')); // trueReflect.set(obj, 'b', 2);console.log(Reflect.ownKeys(obj)); // ['a', 'b']
Language Internals
Section titled “Language Internals”Execution Context
Section titled “Execution Context”// Understanding 'this' bindingconst obj = { name: 'Object', method() { console.log(this.name); // 'Object'
const arrow = () => { console.log(this.name); // 'Object' (inherits from method) };
function regular() { console.log(this.name); // undefined (strict mode) }
arrow(); regular(); }};
Type Coercion Rules
Section titled “Type Coercion Rules”// Understanding implicit conversionsconsole.log('' + 1); // '1' (string)console.log(+'123'); // 123 (number)console.log(!!'hello'); // true (boolean)console.log([] + []); // '' (empty string)console.log({} + []); // '[object Object]'console.log(null == undefined); // trueconsole.log(null === undefined); // false
Browser Compatibility
Section titled “Browser Compatibility”Feature Detection
Section titled “Feature Detection”// Check for feature supportif ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js');}
if (typeof Promise !== 'undefined') { // Use Promises} else { // Use polyfill or fallback}
// Modern feature detectionconst supportsOptionalChaining = (() => { try { new Function('return obj?.prop')(); return true; } catch { return false; }})();
Polyfills and Transpilation
Section titled “Polyfills and Transpilation”// Example polyfill for Array.includes()if (!Array.prototype.includes) { Array.prototype.includes = function(searchElement, fromIndex) { return this.indexOf(searchElement, fromIndex) !== -1; };}
Best Practices
Section titled “Best Practices”Code Organization
Section titled “Code Organization”// Use modules for organizationexport const formatDate = date => new Intl.DateTimeFormat('en-US').format(date);
export const debounce = (fn, delay) => { let timeoutId; return (...args) => { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn.apply(this, args), delay); };};
// main.jsimport { formatDate, debounce } from './utils.js';
Error Handling
Section titled “Error Handling”// Comprehensive error handlingclass CustomError extends Error { constructor(message, code) { super(message); this.name = 'CustomError'; this.code = code; }}
async function robustOperation() { try { const result = await riskyOperation(); return result; } catch (error) { if (error instanceof CustomError) { console.error(`Custom error ${error.code}: ${error.message}`); } else if (error instanceof TypeError) { console.error('Type error:', error.message); } else { console.error('Unexpected error:', error); } throw error; // Re-throw if needed }}
Debugging and Development
Section titled “Debugging and Development”Console API
Section titled “Console API”// Advanced console usageconsole.log('Basic log');console.warn('Warning message');console.error('Error message');console.info('Info message');
// Groupingconsole.group('User Details');console.log('Name: John');console.log('Age: 30');console.groupEnd();
// Tablesconsole.table([ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]);
// Timingconsole.time('operation');// ... some operationconsole.timeEnd('operation');
// Assertionsconsole.assert(1 === 1, 'This will not show');console.assert(1 === 2, 'This will show as error');
Debugging Techniques
Section titled “Debugging Techniques”// Debugger statementfunction complexFunction(data) { debugger; // Execution will pause here when DevTools is open return data.map(item => item * 2);}
// Stack tracesfunction a() { b(); }function b() { c(); }function c() { console.trace('Call stack'); }a();
This reference is continuously updated to reflect the latest JavaScript specifications and best practices. Use the search function to quickly find specific topics.