JavaScript Summary Notes
- Introduction to JavaScript
- Versatile, dynamically typed programming language.
- Supports both client-side and server-side development.
- Integrates with HTML, CSS, and a rich standard library.
- Language Characteristics
- Single-threaded: Executes one task at a time.
- Interpreted: Code runs line by line.
- Dynamically typed: Variable types are determined at run-time.
- Hello, World! Example
- In Browser:
<script>
console.log("Hello, World!");
</script>
- Written in <script> tag.
- Output shown in browser's developer console.
- In Server (Node.js):
console.log("Hello, World!");
- Saved in .js file and run via Node.js terminal.
- JavaScript Comments
- Single-line: //
- Multi-line: /* ... */
- Key Features
- Client-side scripting: Runs in browser.
- Versatile: Supports a wide range of tasks.
- Event-driven: Responds to user actions.
- Asynchronous: Non-blocking, e.g., AJAX, fetch.
- Rich ecosystem: Libraries/frameworks (React, Angular, Vue.js).
- Client-Side vs Server-Side
- Client-Side:
- Interacts with DOM, handles user input.
- Examples: React, Angular, Vue.
- Server-Side:
- Interacts with DBs, files, APIs.
- Uses Node.js, Express.js.
- Programming Paradigms
- Imperative: Focus on how to perform tasks (procedural, OOP).
- Declarative: Focus on what needs to be done (e.g., arrow functions, JSX).
- Applications
- Web Development: Interactive UIs.
- Web Apps: Google Maps, Gmail.
- Server Apps: APIs, backend logic.
- Game Development: HTML5 + JS.
- Smart Devices: Pebble JS for smartwatches.
- Limitations
- Security risks: XSS attacks possible.
- Performance: Slower than compiled languages for heavy tasks.
- Complexity: Advanced use requires deep knowledge.
- Weak typing: Can cause runtime errors.
- Why JavaScript is Lightweight
- Low CPU usage, minimalist syntax.
- No need for explicit data types.
- Runs efficiently in browsers.
- Easy to learn and implement.
- Compiled or Interpreted?
- Both: Modern JS engines use Just-In-Time (JIT) compilation.
- Interpretation: Executes line-by-line initially.
- Hot Code Detection: Identifies frequently used code.
- Compilation: Converts hot code to machine code.
- Execution: Machine code runs faster than interpreted code.