Featureintro
What is JavaScript?
Also called JavaScript, JS, ECMAScript language overview.
JavaScript is a dynamic, prototype-based language with first-class functions. It runs inside a host, such as a browser or Node.js, which gives it input and output.
- Language
- JavaScript
Synopsis
ECMA-262 describes the language in one sentence: "ECMAScript is an object-oriented programming language for performing computations and manipulating computational objects within a host environment."1 MDN calls JavaScript "a prototype-based, garbage-collected, dynamic language, supporting multiple paradigms such as imperative, functional, and object-oriented".2
| Trait | What it means in JavaScript | Rule |
|---|---|---|
| dynamic typing | a variable has no type; a value has one | r3 |
| seven primitive types | Undefined, Null, Boolean, Number, BigInt, String, Symbol; all else is an Object | r4 |
| prototype-based | an object inherits from another object, through a prototype chain | r5, r6 |
| first-class functions | a function is an object that can be passed and returned | r7 |
| hosted | input, output, timers and the event loop come from the host | r1, r8 |
Semantics
r1. The language does no input or output by itself. ECMA-262 says that "there are no provisions in this specification for input of external data or output of computed results." The host environment provides those objects.1 MDN puts it this way: without APIs of the runtime, "most notably console.log() ", "a JavaScript program's behavior is entirely unobservable."3
r2. MDN describes JavaScript as "a lightweight interpreted (or just-in-time compiled) programming language with first-class functions" (Just-in-time compilation).2 Whether the code is interpreted or compiled is a choice of the engine, not of the language.
r3. Types belong to values. In MDN's words, types "are only associated with values, but not with variables", so a let variable can hold a number and then a string.3 ECMA-262 says the syntax is relaxed: "a variable is not required to have its type declared nor are types associated with properties" (Static and dynamic typing).1 Example: dynamic.
r4. A value is either a primitive or an object. The primitive types are Undefined, Null, Boolean, Number, BigInt, String and Symbol. An object is a member of the type Object, and "a function is a callable object."1 The Number type is "an IEEE 754 64-bit double-precision floating point value", so 3 / 2 is 1.5.3 Example: dynamic.
r5. Objects are not built from classes. "Even though ECMAScript includes syntax for class definitions, ECMAScript objects are not fundamentally class-based such as those in C++, Smalltalk, or Java." Each object has a prototype, which may have its own prototype, and so on; this is the prototype chain. A property lookup checks the object first and then each prototype in turn (Prototype-based programming).1 Example: prototype.
r6. "Unlike most class-based object languages, properties can be added to objects dynamically by assigning values to them." The class syntax that ECMAScript 2015 added builds the same constructor and prototype pattern that the built-in objects use.1 Example: prototype.
r7. Functions are values. MDN says JavaScript "supports object-oriented programming with object prototypes and classes", and functional programming "since functions are first-class objects" (Multi-paradigm programming).3 Example: paradigms.
r8. "JavaScript is single-threaded by nature." Asynchronous code runs on an event loop, with callbacks, promises or async and await (Event-driven programming).3 Memory is managed by a garbage collector (Garbage collection).2
r9. The language was first meant for web pages. In a browser, "there is no need for a main program": the code reacts to events from the user.1 It also runs outside browsers, "such as Node.js, Apache CouchDB and Adobe Acrobat".2 The engines of the main browsers are V8 (Chrome), SpiderMonkey (Firefox) and JavaScriptCore (Safari); Node.js and Deno use V8, and Bun uses JavaScriptCore.4
r10. The syntax looks like Java on purpose: "ECMAScript syntax intentionally resembles Java syntax."1 The type system and the object model are not Java's. MDN says JavaScript "does not have Java's static typing and strong type checking", and that it "has a prototype-based object model instead of the more common class-based object model."5
Examples
Dynamic typing and dynamic objects (rules r3, r4 and r6). The variable changes type, the object gains a property and loses one, and the numbers are floating point.
let value = 42;
console.log(typeof value);
value = "forty-two";
console.log(typeof value);
const point = { x: 1 };
point.y = 2;
delete point.x;
console.log(point);
console.log(3 / 2, 7 % 2, 2 ** 10, 0.1 + 0.2);
number
string
{ y: 2 }
1.5 1 1024 0.30000000000000004
Inheritance from an object, then the same with class (rules r5 and r6). dog has no describe property of its own; the lookup finds it on the prototype animal.
const animal = {
describe() {
return `${this.name} says ${this.sound}`;
},
};
const dog = Object.create(animal);
dog.name = "Rex";
dog.sound = "woof";
console.log(dog.describe());
console.log(Object.getPrototypeOf(dog) === animal);
console.log(Object.hasOwn(dog, "describe"), "describe" in dog);
class Cat {
constructor(name) {
this.name = name;
}
describe() {
return `${this.name} says meow`;
}
}
const tom = new Cat("Tom");
console.log(tom.describe(), Object.getPrototypeOf(tom) === Cat.prototype);
Rex says woof
true
false true
Tom says meow true
Three styles in one program (rule r7): a loop that updates a variable, functions passed to filter and map, and a closure that keeps its own count (Closure, Higher-order function).
const scores = [72, 95, 88, 61];
// imperative: a loop that updates a variable
let best = -Infinity;
for (const s of scores) {
if (s > best) best = s;
}
// functional: functions passed as values
const passed = scores.filter((s) => s >= 70).map((s) => `${s}%`);
// a closure keeps its own state
function counter() {
let n = 0;
return () => ++n;
}
const next = counter();
next();
console.log(best, passed, next());
95 [ '72%', '95%', '88%' ] 2
Pitfalls
Type errors come at run time
No compiler checks the types before the program runs (rule r3). A missing property is undefined, and the error comes only when the code calls a method on it. Here the first line runs and prints ADA, and the second throws a TypeError.
This code raises TypeError, on purpose.
const user = { name: "Ada" };
console.log(user.name.toUpperCase());
console.log(user.email.toUpperCase());
Test for the value first, or use optional chaining, user.email?.toUpperCase(), which gives undefined instead of an error.
+ joins strings, and == converts
"If you add a string to a number (or other value) everything is converted into a string first." The == operator also converts its operands, and === does not, so MDN says === "is usually preferred".3
console.log("3" + 4 + 5, 3 + 4 + "5", "5" - 1);
console.log(123 == "123", 123 === "123");
console.log(Number("3") + 4);
345 75 4
true false
7
JavaScript is not Java
The two names are alike and the syntax is alike (rule r10), but MDN lists the differences: in JavaScript "Variable data types are not declared (dynamic typing, loosely typed)", while in Java "Variable data types must be declared (static typing, strongly typed)".5 Code that looks like Java can still mean something else, as "5" - 1 above shows.
Comparison
Java is class-based. MDN's table says that in Java "Objects are divided into classes and instances with all inheritance through the class hierarchy", and "Classes and instances cannot have properties or methods added dynamically."5 In JavaScript an object inherits from another object, and a property can be added at any time (rule r6).
Lua is also a hosted language. Its manual says: "As an extension language, Lua has no notion of a "main" program: it works embedded in a host client".6 JavaScript has the same split between the language and the host (rule r1), and in a browser it also has no main program (rule r9).
References
- ECMAScript 2026 Language Specification, 4 Overview; 4.1 Web Scripting; 4.3 ECMAScript Overview; 4.3.1 Objects
- JavaScript, MDN, introduction
- JavaScript language overview, MDN, Data types; Numbers; Variables; Operators; Asynchronous programming; Language and runtime
- JavaScript technologies overview, MDN, JavaScript implementations
- Introduction, MDN JavaScript Guide, What is JavaScript?; JavaScript and Java
- Lua 5.4 Reference Manual, 1 Introduction
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag S84VJG Edit this page History Report an error Markdown