JavaScript

On this page 7

JavaScript is a high-level, dynamically typed, multi-paradigm programming language standardized as ECMAScript, and it is the only language that runs natively in every web browser. Among programming languages, it is defined by its event-driven, single-threaded execution model, its prototype-based objects and its first-class functions.

Created in 1995 for adding interactivity to web pages, JavaScript now also runs on servers through runtimes such as Node.js, Deno and Bun, in desktop applications, in mobile frameworks and in embedded scripting engines. Despite the similar name, it is unrelated to Java in design and implementation.

Language characteristics of JavaScript

JavaScript combines ideas from several traditions. Its syntax resembles C and Java, its functions behave like those in Scheme, and its object model follows prototype-based inheritance from the Self language. Classes, introduced in 2015, are syntax on top of prototypes rather than a separate model.

CharacteristicWhat it meansPractical consequence
Dynamic typingTypes belong to values, not variablesType errors appear at run time
Weak typing with coercionValues convert implicitly in operationsSurprising comparisons with loose equality
First-class functionsFunctions are valuesCallbacks, closures and functional patterns
Prototype-based objectsObjects inherit from other objectsFlexible but unusual inheritance model
Single-threaded event loopOne call stack per agentNon-blocking I/O through asynchronous code
Garbage collectionMemory is managed automaticallyNo manual freeing, but leaks are still possible
Core characteristics of JavaScript

The language is defined by the ECMA-262 specification, maintained by the TC39 committee. Since ES2015, also called ES6, a new edition appears every year, and features advance through a public stage process from proposal to inclusion.

How JavaScript code runs

A JavaScript engine such as V8, SpiderMonkey or JavaScriptCore parses source code and executes it with a combination of an interpreter and just-in-time compilers. Frequently executed functions are compiled to optimized machine code based on observed types, and are deoptimized if those assumptions break.

Concurrency relies on the event loop. The engine runs one task at a time to completion, then processes microtasks such as resolved promises, then takes the next task from the queue, such as a timer, a network response or a click. Long synchronous code therefore blocks the whole page, which is why heavy work is moved to Web Workers or split into smaller pieces.

Asynchronous programming evolved in three steps, summarized in the table.

StyleIntroducedStrengthWeakness
CallbacksFrom the startSimple and universalDeep nesting and scattered error handling
PromisesES2015Chaining and compositionVerbose chains
async and awaitES2017Reads like sequential codeEasy to serialize work accidentally
Asynchronous styles in JavaScript

Where JavaScript is used

The same language serves very different environments. What differs is the host environment: the set of APIs available besides the core language.

EnvironmentHost APIsTypical use
Web browserDOM, fetch, storage, Web WorkersInteractive web pages and applications
Node.js, Deno, BunFile system, network, processesServers, command-line tools, build tooling
Desktop frameworksBrowser engine plus native accessCross-platform desktop applications
Mobile frameworksBridges to native componentsCross-platform mobile applications
Edge and serverless platformsRequest and response handlersLightweight functions near users
JavaScript runtime environments

The npm registry is the largest package ecosystem for any language, which speeds up development but also creates long dependency chains that require careful security maintenance.

JavaScript compared with other languages

JavaScript is most often compared with TypeScript, a typed superset that compiles to JavaScript, and with other general-purpose languages that compete on the server.

LanguageTypingMain runtimeRelation to JavaScript
TypeScriptStatic, optional, structuralCompiles to JavaScriptAdds types, same runtime behavior
PythonDynamic, strongInterpreterSimilar flexibility, different ecosystem
JavaStatic, nominalJVMUnrelated despite the name
PHPDynamic, gradualServer interpreterTraditional server alternative
GoStaticNative compiledCommon choice for high-concurrency servers
JavaScript and neighboring languages

The main argument for JavaScript on the server is one language across the stack, which lets teams share code and skills between browser and server. The main argument against it is weaker static guarantees, which is why large projects frequently adopt TypeScript.

Pitfalls that experienced developers watch for

  • Loose equality: == applies type coercion, so 0 == '' is true; using strict equality === avoids this class of bugs.
  • Floating-point arithmetic: all ordinary numbers are 64-bit IEEE 754 values, so 0.1 + 0.2 is not exactly 0.3; money calculations should use integers of cents or BigInt.
  • The value of this: it depends on how a function is called, not where it is defined; arrow functions capture it lexically.
  • Unhandled promise rejections: a forgotten await or missing error handler lets failures pass silently or crash a Node.js process.
  • Blocking the event loop: a long loop or synchronous file access freezes the page or stalls every request on a server.
  • Supply-chain risk: small packages with many dependents are attractive targets, so lock files and dependency audits are essential.

In simple terms

JavaScript is the language that makes web pages interactive. When a menu opens, a form checks your input or a map moves as you drag it, JavaScript is usually doing the work inside your browser.

If a web page were a house, HTML would be the walls and rooms, CSS the paint and furniture, and JavaScript the electricity and plumbing that make things respond when you use them. The key takeaway is that JavaScript now runs far beyond the browser, including on the servers behind many websites.

A practical example

A small team builds a booking system for a chain of fitness studios. The customer-facing site shows class schedules that update without reloading, and the server checks capacity and stores reservations. The team writes both parts in JavaScript: the browser code renders the schedule and the server runs on Node.js with a database.

During testing, the site slows down when many members book at the same time. Profiling shows that the server builds a large monthly report synchronously on every booking, which blocks the event loop for about 300 milliseconds. Moving the report to a background job brings response times back under 50 milliseconds, and the team later adds TypeScript to catch data shape errors shared between browser and server.