JavaScript Fundamentals
A comprehensive guide to core JavaScript concepts, modern ES6+ features, asynchronous programming patterns, and industry best practices for building robust applications.
When to Use This Skill
This skill is essential when:
- Writing JavaScript Code: Building web applications, Node.js backends, or any JavaScript-based project
- Code Reviews: Evaluating code quality, identifying anti-patterns, suggesting improvements
- Teaching/Mentoring: Explaining JavaScript concepts, debugging issues, pair programming
- Refactoring Legacy Code: Modernizing codebases with ES6+ features and better patterns
- Architecture Design: Choosing appropriate patterns and structures for your application
- Performance Optimization: Understanding memory management, event loops, and efficient patterns
- Debugging: Tracing execution flow, understanding scope chains, and async behavior
- Interview Preparation: Mastering fundamental concepts tested in technical interviews
Core Concepts
Variables and Scope
JavaScript has three ways to declare variables, each with different scoping rules:
var: Function-scoped, hoisted, can be redeclared
let: Block-scoped, not hoisted to usable state, cannot be redeclared
const: Block-scoped, must be initialized, reference cannot be reassigned
Best Practice: Use const by default, let when reassignment is needed, avoid var.
Data Types
JavaScript has 7 primitive types and objects:
Primitives:
String: Text dataNumber: Integers and floating-point numbersBigInt: Arbitrary precision integersBoolean: true/falseundefined: Uninitialized variablenull: Intentional absence of valueSymbol: Unique identifiers
Objects: Collections of key-value pairs, including arrays, functions, dates, etc.
Functions
Functions are first-class citizens in JavaScript - they can be assigned to variables, passed as arguments, and returned from other functions.
Function Declaration:
Function Expression:
Arrow Function (ES6+):
Key Differences:
- Arrow functions don't have their own
thisbinding - Arrow functions cannot be used as constructors
- Arrow functions don't have
argumentsobject - Function declarations are hoisted, expressions are not
Objects and Arrays
Object Creation:
Array Methods:
Closures
A closure is a function that has access to variables in its outer (enclosing) lexical scope, even after the outer function has returned.
Use Cases:
- Data privacy/encapsulation
- Factory functions
- Event handlers
- Callbacks maintaining state
- Module pattern implementation
Prototypes and Inheritance
JavaScript uses prototypal inheritance. Every object has an internal [[Prototype]] link to another object.
Classes (ES6+)
Classes provide syntactic sugar over prototypal inheritance:
Modern JavaScript (ES6+)
Destructuring
Extract values from arrays or properties from objects:
Spread and Rest Operators
Spread (...) expands elements:
Rest (...) collects elements:
Template Literals
Multi-line strings and string interpolation:
Optional Chaining and Nullish Coalescing
Optional Chaining (?.): Safely access nested properties:
Nullish Coalescing (??): Default values for null/undefined:
Modules
Exporting:
Importing:
Asynchronous Patterns
The Event Loop
JavaScript is single-threaded but handles async operations through the event loop:
- Call Stack: Executes synchronous code
- Web APIs: Browser/Node APIs (setTimeout, fetch, etc.)
- Callback Queue: Completed async operations wait here
- Event Loop: Moves callbacks from queue to stack when stack is empty
Callbacks
Traditional async pattern (can lead to "callback hell"):
Promises
Promises represent eventual completion (or failure) of an async operation:
Async/Await
Modern syntax for handling promises (ES2017+):
Error Handling in Async Code
Common Patterns
Module Pattern
Encapsulate private data and expose public API:
Revealing Module Pattern
Cleaner variation of module pattern:
Factory Pattern
Create objects without specifying exact class:
Singleton Pattern
Ensure only one instance exists:
Observer Pattern
Subscribe to and publish events:
Memoization
Cache function results for performance:
Best Practices
Naming Conventions
Error Handling
Performance Tips
Code Organization
Memory Management
Testing Considerations
Quick Reference
Array Methods Cheat Sheet
Object Methods Cheat Sheet
Common Gotchas
This comprehensive guide covers the essential JavaScript fundamentals needed for modern development. Practice these concepts regularly and refer to the EXAMPLES.md file for detailed implementation scenarios.



