JavaScript Explained: The Language That Runs the Whole Web
Introduction
JavaScript is the only language that runs natively in every web browser, and thanks to Node.js (covered in an earlier post) it also runs on servers. That makes it the most widely deployed programming language in the world. The earlier async/await post covered waiting for things; this one covers the fundamentals everything else is built on.
Variables: let, const, and the var you should avoid
Modern JavaScript has two ways to declare a variable, and the choice matters:
let score = 0; score = 10; // Fine — let can be reassigned const name = "Sara"; // name = "Ali"; // Error — const cannot be reassigned // var exists for historical reasons. Avoid it: it leaks out of blocks // in surprising ways that let and const do not.
Use const by default, let when you know the value will change, and forget var exists. That single habit prevents a whole class of bugs.
Functions: the building blocks
Functions in JavaScript are values, just like numbers and strings — you can store them, pass them around, and return them from other functions. That one idea powers callbacks, event handlers, and most of modern web development:
function greet(name) {
return `Hello, ${name}!`;
}
const greetArrow = (name) => `Hello, ${name}!`;
console.log(greet("Sara")); // Hello, Sara!
console.log(greetArrow("Ali")); // Hello, Ali!
Arrow functions (=>) are a shorter way to write the same thing, and they are now the standard style in React and most modern codebases. The backtick template literal (`Hello, ${name}!`) inserts variables directly into text — the same interpolation idea as C# and Swift.
Arrays and objects: the two containers
Almost all JavaScript data lives in two shapes. Arrays hold ordered lists; objects hold named values:
const numbers = [1, 2, 3];
const doubled = numbers.map((n) => n * 2); // [2, 4, 6]
const user = { name: "Sara", age: 25 };
console.log(user.name); // Sara
map transforms every item in an array with a small function — the same idea as LINQ's Select in C# and Python's list comprehensions. Learn map, filter, and find, and you can read most modern JavaScript.
The event loop, in thirty seconds
JavaScript runs one thing at a time, yet web pages stay responsive. The trick is the event loop: slow work (like fetching data) is handed off, and when it finishes, its callback is queued to run. That is why async/await works, why buttons stay clickable during a download, and why you should never write a long blocking loop on the main thread.
Takeaway
const by default, let when it changes, functions as values, arrays and objects as your two containers, and the event loop keeping everything responsive. Those five ideas are the foundation under React, Vue, Angular, Node, and every framework in this series.
Comments
Post a Comment