javascript - dwilson2547/wiki_demo GitHub Wiki
JavaScript (JS) is a high-level, dynamic, and interpreted programming language primarily used for client-side web development. It enables interactive and dynamic content on websites, such as animations, form validation, and real-time updates. JavaScript is also used on the server-side (Node.js) and for mobile/desktop apps (React Native, Electron).
- 1. Core Features
- 2. Key Concepts
- 3. JavaScript in the Browser
- 4. JavaScript Outside the Browser
- 5. Modern JavaScript (ES6+)
- 6. Frameworks and Libraries
- 7. Strengths and Weaknesses
- 8. Example: Simple Web App
-
Dynamic Typing: Variables can hold any data type (e.g.,
let x = 5; x = "hello";
). - Prototype-Based: Objects inherit properties from other objects (no classical inheritance).
- First-Class Functions: Functions are treated as objects (can be passed as arguments, returned from other functions).
-
Asynchronous Programming: Supports callbacks, promises, and
async/await
for handling async operations (e.g., API calls). - Event-Driven: Responds to user actions (e.g., clicks, keypresses) via event listeners.
-
Variables: Declared with
let
,const
, orvar
.let age = 25; // Mutable const PI = 3.14; // Immutable var oldVar = "legacy"; // Avoid (hoisting issues)
-
Primitive Types:
-
string
,number
,boolean
,null
,undefined
,symbol
,bigint
.
-
-
Non-Primitive Types:
-
object
(e.g., arrays, functions, dates).
-
-
Function Declarations:
function greet(name) { return `Hello, ${name}!`; }
-
Arrow Functions (ES6+):
const greet = (name) => `Hello, ${name}!`;
-
Higher-Order Functions: Functions that take/return other functions (e.g.,
map
,filter
).
-
Objects: Key-value pairs.
const person = { name: "Alice", age: 25 };
-
Arrays: Ordered lists.
const fruits = ["apple", "banana"]; fruits.push("orange"); // Add item
-
Callbacks:
setTimeout(() => console.log("Delayed"), 1000);
-
Promises:
fetch("https://api.example.com") .then(response => response.json()) .catch(error => console.error(error));
-
Async/Await (ES2017):
async function fetchData() { const response = await fetch("https://api.example.com"); const data = await response.json(); return data; }
JavaScript interacts with the Document Object Model (DOM) to dynamically update HTML/CSS:
document.getElementById("myButton").addEventListener("click", () => {
document.body.style.backgroundColor = "blue";
});
-
Import/Export:
// math.js export const add = (a, b) => a + b; // app.js import { add } from './math.js';
- Engine: Executed by browser engines (e.g., V8 in Chrome, SpiderMonkey in Firefox).
-
APIs:
- DOM API: Manipulate HTML/CSS.
- Fetch API: Make HTTP requests.
-
Web Storage:
localStorage
andsessionStorage
. - WebSockets: Real-time communication.
-
Node.js: Server-side runtime for building backend services.
const http = require('http'); http.createServer((req, res) => res.end("Hello!")).listen(3000);
- Electron: Build cross-platform desktop apps (e.g., VS Code, Slack).
- React Native: Develop mobile apps for iOS/Android.
-
ES6 (2015): Introduced
let/const
, arrow functions, classes, modules, promises. -
ES2020+: Added optional chaining (
?.
), nullish coalescing (??
), and more. -
Example (ES6 Class):
class Person { constructor(name) { this.name = name; } greet() { return `Hi, ${this.name}!`; } }
Tool | Use Case |
---|---|
React | UI components (SPAs). |
Vue.js | Progressive UI frameworks. |
Angular | Full-featured frontend framework. |
Express.js | Backend APIs (Node.js). |
Next.js | Server-rendered React apps. |
jQuery | DOM manipulation (legacy). |
Strengths | Weaknesses |
---|---|
✅ Easy to learn and use. | ❌ Dynamic typing can lead to bugs. |
✅ Runs in all browsers. | ❌ Single-threaded (but async helps). |
✅ Large ecosystem (npm, frameworks). | ❌ Performance bottlenecks for CPU-heavy tasks. |
✅ Asynchronous programming. | ❌ Callback hell (mitigated by promises/async). |
<!DOCTYPE html>
<html>
<body>
<button id="myButton">Click Me</button>
<script>
document.getElementById("myButton").addEventListener("click", () => {
alert("Button clicked!");
});
</script>
</body>
</html>
JavaScript is the lingua franca of the web, powering everything from simple scripts to complex applications. Its versatility (frontend, backend, mobile) and ecosystem (npm, frameworks) make it one of the most popular languages today. Modern features like ES6+, async/await, and modules have significantly improved its capabilities.