JavaScript is one of the most widely used programming languages in the world, powering the web with interactivity, backend services, and even mobile and desktop applications. This article provides a complete overview of JavaScript, including its architecture, internal workings, syntax, and practical examples.
1. JavaScript Architecture
JavaScript is primarily a high-level, interpreted, and dynamically typed language. It was initially designed to run in the browser but now also runs on the server with environments like Node.js. Its architecture can be understood in three main layers:
1.1 Engine
- JavaScript is executed by JavaScript engines.
-
Example engines:
- V8 (Chrome, Node.js)
- SpiderMonkey (Firefox)
- JavaScriptCore (Safari)
The engine interprets or compiles JS code into machine code and executes it.
1.2 Event Loop
JavaScript uses a single-threaded non-blocking event loop, which allows asynchronous operations without blocking the main thread.
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
console.log("End");
// Output: Start, End, Timeout
- The event loop handles callbacks, promises, and asynchronous events.
- This allows concurrency without multithreading.
1.3 Call Stack and Heap
- Call Stack: Stores execution context of functions.
- Heap: Stores objects and dynamic memory.
function a() { console.log("A"); }
function b() { a(); console.log("B"); }
b(); // Call stack: b -> a
2. JavaScript Internal Concepts
2.1 Execution Context
- Every function or code block runs in an execution context.
-
Components:
- Variable Environment
- Scope Chain
- This binding
2.2 Hoisting
- Variables and functions are hoisted to the top of their scope.
console.log(x); // undefined
var x = 5;
foo(); // Works
function foo() { console.log("Hello"); }
2.3 Scope
- Global Scope
- Function Scope
-
Block Scope (
let,const)
let a = 10; // global
{
let b = 20; // block
}
2.4 Closures
- A function that remembers its outer lexical scope.
function outer() {
let count = 0;
return function inner() {
count++;
return count;
}
}
const counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
2.5 Prototypes and Inheritance
- Every object has a prototype. Functions have a
prototypeproperty.
function Person(name) { this.name = name; }
Person.prototype.sayHello = function() { console.log("Hello " + this.name); }
const p = new Person("Alice");
p.sayHello(); // Hello Alice
3. JavaScript Data Types
3.1 Primitive Types
NumberStringBooleanNullUndefinedSymbol-
BigInt
let num = 42; // Number
let str = "Hello"; // String
let flag = true; // Boolean
3.2 Reference Types (Objects)
- Objects, Arrays, Functions, Dates, Regex, etc.
const obj = {name: "John", age: 30};
const arr = [1, 2, 3];
4. JavaScript Operators
- Arithmetic:
+,-,*,/,%,** - Assignment:
=,+=,-= - Comparison:
==,===,!=,!==,>,< - Logical:
&&,||,! - Bitwise:
&,|,^,~,<<,>>
5. Control Structures
5.1 Conditional Statements
if (true) { console.log("Yes"); }
else if (false) { console.log("No"); }
else { console.log("Maybe"); }
switch (1) {
case 1: console.log("One"); break;
default: console.log("Other");
}
5.2 Loops
for (let i = 0; i < 5; i++) console.log(i);
let j = 0;
while (j < 3) { console.log(j); j++; }
do { console.log("Do While"); } while(false);
6. Functions
6.1 Function Declaration
function greet(name) { return "Hello " + name; }
console.log(greet("Alice"));
6.2 Function Expression
const greet = function(name) { return "Hi " + name; }
6.3 Arrow Functions
const greet = (name) => "Hi " + name;
6.4 Higher-Order Functions
const numbers = [1,2,3];
const doubled = numbers.map(n => n*2);
7. Objects and Classes
7.1 Object Literals
const person = {name: "Alice", age: 25};
console.log(person.name);
7.2 ES6 Classes
class Person {
constructor(name) { this.name = name; }
greet() { console.log("Hello " + this.name); }
}
const p = new Person("Alice");
p.greet();
7.3 Inheritance
class Employee extends Person {
constructor(name, job) { super(name); this.job = job; }
}
8. Arrays
let arr = [1,2,3];
arr.push(4); // Add element
arr.pop(); // Remove last
arr.shift(); // Remove first
arr.unshift(0); // Add at start
arr.map(x => x*2);
9. Asynchronous JavaScript
9.1 Callbacks
function fetchData(callback) { setTimeout(() => callback("Data"), 1000); }
fetchData(console.log);
9.2 Promises
const p = new Promise((resolve, reject) => resolve("Success"));
p.then(console.log);
9.3 Async/Await
async function getData() {
const result = await p;
console.log(result);
}
getData();
10. Modules
- ES6 Modules
// export.js
export const pi = 3.14;
// import.js
import { pi } from './export.js';
- CommonJS (Node.js)
module.exports = { pi: 3.14 };
const { pi } = require('./export.js');
11. Error Handling
try {
throw new Error("Oops!");
} catch(e) {
console.log(e.message);
} finally {
console.log("Always runs");
}
12. JavaScript Built-in Objects
-
Math,Date,RegExp,JSON,Array,String,Object,Set,Map
console.log(Math.random());
console.log(new Date().toISOString());
13. Browser APIs
- DOM Manipulation
document.getElementById("myDiv").innerHTML = "Hello";
- Fetch API
fetch("https://api.example.com/data")
.then(res => res.json())
.then(console.log);
- LocalStorage
localStorage.setItem("name", "Alice");
console.log(localStorage.getItem("name"));
14. Conclusion
JavaScript is a powerful, versatile, and ubiquitous language. From the browser to the server, from frontend frameworks like React, Vue, and Angular to backend platforms like Node.js, JavaScript is everywhere. Understanding its architecture, internals, and all syntax elements allows developers to write efficient, clean, and scalable code.
Top comments (0)