Classes are no magic: class and the prototype chain, explained plainly
Each dish on the menu is an instance, orders split into plain and delivery, members into plain and VIP — the inheritance lives in these classes. This guide follows MDN's Using classes and Inheritance and the prototype chain guides: constructors, methods and prototypes, private fields, accessors, statics, inheritance, prototype-chain lookup, and the monkey-patching red line.

First, the boundaries of this guide. The site already has a practical guide to destructuring assignment, about splitting an object or an array into several variables in one go; a practical guide to array methods, about how to process a list you hold; and a practical guide to closures and scope, about who can see a variable. This guide is about none of those. It is about one thing only: what a class is, where methods actually live, and which chain a missing property is searched along. Used well, a class gathers the data and behavior of one kind of thing in a single place; used badly, one twist of the inheritance chain changes everything at once. The "Greenstone Cafe" used below is still the same fictional practice project: a small web tool that takes orders for a street-side cafe. This time it is about its menu, orders, and members: each dish on the menu is an instance, orders split into plain orders and delivery orders, and members split into plain members and VIP members — the inheritance lives in these classes. Every operation, code sample, and number built around it was invented to explain the technique, not a real record. All syntax and behavior of classes and the prototype chain comes from MDN's Using classes guide and its Inheritance and the prototype chain guide; see each article's meta information and evidence record for sources. MDN's own examples, such as the Date, the Color, the Box, and the Graph, are the documentation's examples, not real user data.
Step 1: A class has three features, and Date is a ready-made example
You can understand what a class is before writing one yourself: JavaScript's built-in Date is a class. MDN's example is: const bigDay = new Date(2019, 6, 19), bigDay.toLocaleDateString() returns a string, bigDay.getTime() returns a number, and Date.now() is called straight from the class itself. Three features show up in this example: first, classes create objects through the new operator; second, each object carries some properties added by the class, data or methods; third, the class itself stores some properties, usually used to interact with instances. These three features correspond to the three key parts of a class: the constructor, instance methods and instance fields, and static methods and static fields. The cafe's menu is modeled this way: each dish is a Dish instance, new Dish("latte", 28) makes one dish, dish.price is its data, dish.format() is its method, and Dish.isValidPrice(28) is a method of the class itself. Remember the first sentence: new makes instances, instances carry data and methods, and the class carries a set of its own.
Step 2: A class declaration looks like this — it is not hoisted, and calling it without new throws
Classes are usually created with class declarations. MDN's example is: class MyClass { constructor() {} myField = "foo"; myMethod() {} static myStaticField = "bar"; static myStaticMethod() {} static {} #myPrivateField = "bar"; }. A class body holds just these things: a constructor, instance fields, instance methods, static fields, static methods, a static block, and the private forms of each. Once declared, you create instances with new: const myInstance = new MyClass(), myInstance.myField is 'foo', and myInstance.myMethod() can be called directly. Two red lines to remember: first, a typical function constructor can be both constructed with new and called without new, but a class cannot — attempting to call a class without new throws a TypeError: Class constructor MyClass cannot be invoked without 'new'; second, unlike function declarations, class declarations are not hoisted, so using a class before it is declared throws a ReferenceError: Cannot access 'MyClass' before initialization, behaving like a variable declared with let or const. Classes also have expression forms: const MyClass = class { }, and the name of a named expression is only visible inside the class body — using that name outside the body throws a ReferenceError. The cafe's Order class stepped into this trap: the file called new Order() at the top and declared the class at the bottom, and the page threw "class not yet initialized" on load. Remember the test: declare a class before using it, and always write new to make an instance.
Step 3: The constructor is the factory that makes instances, and this is the new instance
The most important job of a class is to act as a "factory" for objects, and instance creation is done by the constructor. MDN's example is the Color class: class Color { constructor(r, g, b) { this.values = [r, g, b]; } }, const red = new Color(255, 0, 0), and red.values is [255, 0, 0]. A constructor's syntax is exactly the same as a normal function, so rest parameters work too: constructor(...values) { this.values = values; }. Each time you call new, a different instance is created: const red = new Color(255, 0, 0), const anotherRed = new Color(255, 0, 0), and red === anotherRed is false. Inside a class constructor, this points to the newly created instance, and the this value is automatically returned as the result of new. MDN warns: do not return any value from the constructor, because if you return a non-primitive value, it will become the value of the new expression, and the value of this is dropped. MDN's example is: a constructor that returns {} makes new MyClass().myField undefined, because this was thrown away. The cafe's Dish class is written this way: class Dish { constructor(name, price) { this.name = name; this.price = price; } }, and every new Dish is a new dish — even with the same name and price, two instances are never equal. Remember the test: a constructor only hangs data on this and returns nothing; every new makes a new instance.
Step 4: Methods live on the prototype, shared by all instances
A class with only a constructor is not much different from a factory function that just creates plain objects; the power of classes is that methods are assigned to instances automatically. MDN's Color example adds a getRed method: class Color { constructor(r, g, b) { this.values = [r, g, b]; } getRed() { return this.values[0]; } }, const red = new Color(255, 0, 0), and red.getRed() is 255. You can also define the method inside the constructor: this.getRed = function () { return this.values[0]; }, and that works too — but the problem is that it creates a new function every time a Color instance is created, even when they all do the same thing. MDN's check is: new Color().getRed === new Color().getRed is false. With the method spelling this does not happen: the method is defined on the prototype of all instances, Color.prototype, so it is shared between all instances, while this differs each time it is called. MDN also adds a setRed: after red.setRed(0), red.getRed() is 0. The cafe's menu cards stepped into this trap: the render function was written inside the constructor, so a hundred dishes meant a hundred identical copies of the same function and wasted memory; rewritten as a method, a hundred instances share one copy. Remember the test: write methods in the class body and all instances share one copy; write them in the constructor and every instance gets a new one.
Step 5: Private fields are truly private, and encapsulation rests on them
What is wrong with accessing values directly? MDN gives the example of changing how a color is represented: if colors were suddenly stored as HSL instead of RGB, red.values[0] would no longer be 255 but the hue value 0 for pure red, and the user's code would break at once. This is the object-oriented idea of "encapsulation": do not touch the underlying implementation of an object, use well-abstracted methods instead. In classes this is done through private fields: an identifier prefixed with #. MDN's example is: class Color { #values; constructor(r, g, b) { this.#values = [r, g, b]; } getRed() { return this.#values[0]; } setRed(value) { this.#values[0] = value; } }. Private fields in JavaScript are hard private: accessing red.#values outside the class is an early syntax error, a SyntaxError, and if the class implements no methods that expose these fields, there is absolutely no mechanism to retrieve them from outside. You can also add checks inside setRed: if value is below 0 or above 255, throw a RangeError: Invalid R value, so red.setRed(1000) throws outright; with values exposed, a user could bypass the check by assigning to values[0] directly. Private fields carry three more rules: a class method can read the private fields of other instances of the same class — red.redDifference(crimson) is 35; when you do not know whether the other object is an instance of the same class, check first with #values in anotherColor; and the same name cannot be declared twice in one class, nor can a private field be deleted — both are early syntax errors. The cafe's member balance is managed this way: balance is a private field, reachable only through the recharge and spend methods, spend throws when the balance is too low, and the road to editing the balance directly is closed. Remember the test: mark internal data with #, put checks inside methods, and never hand raw data to the user.
Step 6: Accessors make methods feel like properties
Reading and writing a value through getRed and setRed is still somewhat unergonomic, and accessor fields let you manipulate something as if it were an "actual property". MDN's example is: class Color { constructor(r, g, b) { this.values = [r, g, b]; } get red() { return this.values[0]; } set red(value) { this.values[0] = value; } }, const red = new Color(255, 0, 0), red.red = 0, and red.red is 0. It looks as if the object has a property called red — but actually, no such property exists on the instance; there are only two methods, prefixed with get and set, which allows them to be manipulated as if they were properties. If a field only has a getter but no setter, it will be effectively read-only. MDN's example is: a Color with only get red(), after red.red = 0, red.red is still 255; in strict mode the assignment throws a type error, "Cannot set property red of #<Color> which has only a getter", while in non-strict mode the assignment is silently ignored. The cafe's order total is written this way: order.total has only a getter, computed inside by reducing the dish prices, so the outside can read it but not write it — to change the total you have to change the dishes in the order. Remember the test: get plus set is a read-write property, get alone is a read-only property; in strict mode, writing to a read-only property throws.
Step 7: Public fields belong to each instance; statics belong to the class
Private fields also have their public counterparts, which allow every instance to have a property. MDN's example is: class MyClass { luckyNumber = Math.random(); }, and new MyClass().luckyNumber differs every time — 0.5, 0.3, anything is possible. Public fields are almost equivalent to assigning a property to this in the constructor: constructor() { this.luckyNumber = Math.random(); }. Fields are usually designed to be independent of the constructor's parameters, fitting things every instance needs whose value does not depend on what was passed in. Static properties are another group: features defined on the class itself, rather than on individual instances — static methods, static fields, and static getters and setters, all with private counterparts. MDN's example is: class Color { static isValid(r, g, b) { return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255; } }, Color.isValid(255, 0, 0) is true and Color.isValid(1000, 0, 0) is false. Statics differ from instance features in only two ways: they are all prefixed with static, and they are not accessible from instances — new Color(0, 0, 0).isValid is undefined. There is also a static initialization block: static { MyClass.myStaticProperty = "foo"; }, a block of code that runs when the class is first loaded, almost equivalent to immediately executing code after the class is declared, except that it has access to static private elements. MDN also notes the good habit of namespacing: prefixing utility methods with what they deal with, such as Number.parseInt instead of the bare parseInt. The cafe's Dish.isValidPrice is a static method: it checks whether a price is legal without making a dish first; each dish's name and price are instance fields, fixed only when the dish is made with new. Remember the test: instance things hang on the instance, class things hang on the class, and instances cannot read statics.
Step 8: Inheritance borrows behavior, and super must be called first
A key feature classes bring about, in addition to ergonomic encapsulation with private fields, is inheritance: one object can "borrow" a large part of another object's behaviors, while overriding or enhancing certain parts with its own logic. MDN's example is a Color that needs to support transparency: rather than adding an alpha field to every instance, create a derived class. The spelling is: class ColorWithAlpha extends Color { #alpha; constructor(r, g, b, a) { super(r, g, b); this.#alpha = a; } get alpha() { return this.#alpha; } set alpha(value) { if (value < 0 || value > 1) { throw new RangeError("Alpha value must be between 0 and 1"); } this.#alpha = value; } }. The language requires calling super() before accessing this: the super() call calls the parent class's constructor to initialize this, so you can have code before super(), but you cannot access this before super() — the language prevents you from accessing the uninitialized this. A derived class inherits all methods from its parent: ColorWithAlpha declares no red accessor, yet new ColorWithAlpha(255, 0, 0, 0.5).red is still 255, because that behavior is specified by the parent class. Derived classes can also override methods from the parent: every class implicitly inherits the Object class, whose base toString() prints [object Object] and is notoriously useless, so a subclass can override it to print the color's RGB values; and super.toString() lets you build on the parent's result, returning "255, 0, 0, 0.5". When you use extends, static methods inherit too, so you can also override or enhance them: ColorWithAlpha.isValid(255, 0, 0, -1) is false. Two red lines: derived classes do not have access to the parent class's private fields — this.#values inside a subclass is a SyntaxError, because private fields are scoped to the class body itself; and a class can only extend from one class, which prevents problems in multiple inheritance like the diamond problem. The cafe's delivery orders are written this way: class DeliveryOrder extends Order, super builds the plain order's fields first, then the delivery address and fee are added; instanceof accepts both sides, deliveryOrder instanceof Order is true. Remember the test: a subclass calls super first and uses this after; private fields are not inherited, and a class extends only one parent.
Step 9: The prototype chain is the map for finding properties
Classes are syntax sugar; underneath them runs the prototype chain. MDN's definition is: JavaScript objects are dynamic "bags" of properties, referred to as own properties, and each object has a link to a prototype object. When trying to access a property of an object, the property will not only be sought on the object, but also on the prototype of the object, the prototype of the prototype, and so on, until either a property with a matching name is found or the end of the prototype chain is reached. MDN's example is: const o = { a: 1, b: 2, __proto__: { b: 3, c: 4 } }, and the chain is { a: 1, b: 2 } pointing to { b: 3, c: 4 }, then to Object.prototype, and finally null. o.a is 1, found on the object itself; o.b is 2, found on the object itself, while the prototype's b is 3 but is never visited — this is called property shadowing; o.c is 4, not an own property, found on the prototype; o.d is undefined, the whole chain walked with nothing found. Chains can run longer: nest another { d: 5 } inside o's prototype and o.d becomes 5, a chain of four links. Setting a property on an object creates an own property, with only one exception: when it is intercepted by a getter or setter. The cafe's member tiers are looked up this way: a VIP member object holds only a name on itself, its discount is found in the member tier on the prototype, then on Object.prototype, and finally null. Remember the test: look for a property on the object itself first, then up the chain; an own property shadows the prototype's; reaching null with nothing found means undefined.
Step 10: In an inherited method, this is the object that borrowed the method
JavaScript does not have "methods" in the form that class-based languages define them; in JavaScript, any function can be added to an object in the form of a property, and an inherited function acts just as any other property, including property shadowing. The key is this: when an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property. MDN's example is: const parent = { value: 2, method() { return this.value + 1; } }, parent.method() is 3; const child = { __proto__: parent }, child.method() is also 3, because when it is called this is child, value is first sought on child, not found there, and then found on the [[Prototype]], parent.value, which is 2. Assign a value to the child: child.value = 4 creates an own property on child that shadows the value on parent, the child object now looks like { value: 4, __proto__: { value: 2, method } }, and calling child.method() again gives 5. The cafe's member discount is computed this way: the plain member's prototype holds a discount method that reads this.level, a VIP member object writes its own level, and calling the same method gives a different discount. Remember the test: this inside a method is always the caller, never the place the method lives; writing a property on the child shadows, it does not change the prototype.
Step 11: A constructor's prototype is the prototype of its instances
The power of prototypes is that we can reuse a set of properties if they should be present on every instance — especially for methods. MDN's box example starts with a naive version: three box objects each store their own getValue, doing the same thing, which is redundant and unnecessary; then a boxPrototype object holds getValue and the three boxes point their prototypes at it; then, since binding __proto__ for every object creation is still very inconvenient, a constructor function is used: function Box(value) { this.value = value; }, Box.prototype.getValue = function () { return this.value; }, const boxes = [new Box(1), new Box(2), new Box(3)]. We say that new Box(1) is an instance created from the Box constructor function. Box.prototype is not much different from the boxPrototype object created previously — it is just a plain object; every instance created from a constructor function will automatically have the constructor's prototype property as its [[Prototype]] — that is, Object.getPrototypeOf(new Box()) === Box.prototype. Constructor.prototype by default has one own property: constructor, which references the constructor function itself — Box.prototype.constructor === Box — which allows one to access the original constructor from any instance. The class spelling matches this exactly: class Box { constructor(value) { this.value = value; } getValue() { return this.value; } }, methods are created on Box.prototype, and MDN's own words are that classes are syntax sugar over constructor functions. One corollary follows: because Box.prototype references the same object as the [[Prototype]] of all instances, we can change the behavior of all instances by mutating Box.prototype. MDN's example is: const box = new Box(1), then Box.prototype.getValue is replaced with a function returning this.value + 1, and box.getValue() is 2. But re-assigning Constructor.prototype is a bad idea for two reasons: the [[Prototype]] of instances created before the reassignment now references a different object from the [[Prototype]] of instances created after it, so mutating one no longer mutates the other; and unless you manually re-set the constructor property, the constructor function can no longer be traced from instance.constructor, which may break user expectation. The cafe's Dish class is maintained this way: Dish.prototype.format hangs on the prototype and all dishes share it; when the dish display format needs changing, one edit to the prototype changes every dish on the site. Remember the test: an instance's prototype is the constructor's prototype; mutating the prototype changes all instances together; never re-assign prototype.
Step 12: Literals come with prototypes, and array methods live on Array.prototype
Some literal syntaxes in JavaScript create instances that implicitly set the [[Prototype]]. MDN's examples are: const object = { a: 1 }, Object.getPrototypeOf(object) === Object.prototype is true; const array = [1, 2, 3], whose [[Prototype]] is Array.prototype; const regexp = /abc/, whose [[Prototype]] is RegExp.prototype. They can be "de-sugared" into their constructor form: new Array(1, 2, 3) and new RegExp("abc"). So "array methods" like map() are simply methods defined on Array.prototype, which is why they are automatically available on all array instances. One more link in the chain is easy to confuse: Constructor.prototype is only useful when constructing instances. It has nothing to do with Constructor.[[Prototype]], which is the constructor function's own prototype, which is Function.prototype — Object.getPrototypeOf(Constructor) === Function.prototype. A typical constructor builds this prototype chain: obj points to Constructor.prototype, then to Object.prototype, then to null; to build longer chains, set the [[Prototype]] of Constructor.prototype with Object.setPrototypeOf(Derived.prototype, Base.prototype), which in class terms is equivalent to using the extends syntax. MDN also names a legacy pattern: Derived.prototype = Object.create(Base.prototype), which re-assigns the prototype property and removes the constructor property, so it can be more error-prone — do not do this, use Object.setPrototypeOf to mutate it instead. The cafe's order list is understood this way: orders.map works directly because the prototype of orders is Array.prototype, and map lives there. Remember the test: literals come with prototypes; array methods live on Array.prototype; Constructor.prototype and the constructor's own prototype are not the same thing.
Step 13: Object.create, setPrototypeOf, and the monkey-patching red line
MDN systematically summarizes the ways to create objects and change their prototype chains. Object literals: const o = { a: 1 }, the chain is o pointing to Object.prototype and then to null; arrays b = ["yo", "sup", "?"], the chain is b pointing to Array.prototype, then Object.prototype, then null; functions f, the chain is f pointing to Function.prototype, then Object.prototype, then null; the __proto__ literal key: const p = { b: 2, __proto__: o }, the chain is p pointing to o, then Object.prototype, then null. Calling Object.create() creates a new object whose [[Prototype]] is the first argument: const b = Object.create(a), the chain is b pointing to a, then Object.prototype, then null; Object.create(null) makes an object with null directly as its prototype, so d.hasOwnProperty is undefined, because d does not inherit from Object.prototype. Object.setPrototypeOf() allows mutating the [[Prototype]] of an existing object, and can even force a prototype onto a prototype-less object created with Object.create(null) or remove the prototype by setting it to null — but MDN warns that you should set the prototype during creation if possible, because setting the prototype dynamically disrupts all optimizations that engines have made to the prototype chain and might cause some engines to recompile your code for de-optimization. The Object.prototype.__proto__ setter is non-standard and deprecated; you should almost always use Object.setPrototypeOf instead. Then there is one misfeature that used to be prevalent: extending Object.prototype or one of the other built-in prototypes, for example defining Array.prototype.myMethod and then using myMethod on all array instances. MDN calls this monkey patching: it risks forward compatibility, because if the language adds this method in the future but with a different signature, your code will break — it has led to incidents like SmooshGate, and can be a great nuisance for the language to advance since JavaScript tries to "not break the web". The only good reason for extending a built-in prototype is to backport the features of newer JavaScript engines. The cafe's code rules carry this line: business code must not touch Array.prototype or Object.prototype; a helper you need goes in a standalone function or on your own class. Remember the test: set the prototype at creation; an Object.create(null) object does not even have hasOwnProperty; never extend built-in prototypes at will.
Step 14: Long chains cost, and hasOwnProperty checks own properties
A prototype chain is not better for being longer. MDN names two costs: first, the lookup time for properties that are high up on the prototype chain can have a negative impact on performance, and this may be significant in code where performance is critical; trying to access nonexistent properties will always traverse the full prototype chain. Second, when iterating over the properties of an object, every enumerable property that is on the prototype chain will be enumerated. To check whether an object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use hasOwnProperty or Object.hasOwn. MDN's Graph example is: function Graph() { this.vertices = []; this.edges = []; }, Graph.prototype.addVertex = function (v) { this.vertices.push(v); }, const g = new Graph(), and the chain is g pointing to Graph.prototype, then Object.prototype, then null. g.hasOwnProperty("vertices") is true and Object.hasOwn(g, "vertices") is true; g.hasOwnProperty("nope") is false; g.hasOwnProperty("addVertex") is false, because addVertex lives on the prototype, and Object.getPrototypeOf(g).hasOwnProperty("addVertex") is true. MDN also notes that it is not enough to check whether a property is undefined: the property might very well exist, but its value just happens to be set to undefined. The whole picture is worth holding at once: all constructor functions in JavaScript have a special property called prototype, which works with the new operator; when you do const a1 = new A(), JavaScript sets a1.[[Prototype]] = A.prototype before running function A() with this defined to it; when you then access properties of the instance, JavaScript first checks whether they exist on that object directly, and if not, it looks in [[Prototype]], recursively, until it is found or Object.getPrototypeOf returns null; all properties defined on prototype are effectively shared by all instances, and you can even later change parts of prototype and have the changes appear in all existing instances. MDN closes with advice on when to use classes: consider using them when you want to create objects that store their own internal data and expose a lot of behavior — the Map and Set classes store a collection of elements and let you access them by key with get(), set(), and has(); the Date class stores a date as a Unix timestamp and lets you format, update, and read individual date components; the Error class stores information about an exception, and TypeError and ReferenceError extend Error, so instanceof tells you at once which kind of error it is. The cafe's member system closes the same way: a member class stores balance and tier and exposes recharge, spend, and discount; the chain stays two or three links long and never longer. Remember the test: the longer the chain, the slower the lookup; check own properties with hasOwnProperty, never by testing for undefined alone.
Step 15: 110 class and prototype-chain incidents in one month — check yourself against the chart

The chart above shows a fictional month for the Greenstone Cafe project: 110 incidents related to classes and the prototype chain in total. Forgetting new and calling the class directly: 28, or 25.5%, the largest group; writing a method inside the constructor, so every instance got a new copy of the function: 22, or 20.0%; using this in a subclass before calling super: 19, or 17.3%; editing private data directly and bypassing the check inside setRed: 16, or 14.5%; changing a method on the prototype, so all instances changed together: 14, or 12.7%; hanging a property on an instance and believing the prototype had changed: 11, or 10.0%. 28 + 22 + 19 + 16 + 14 + 11 = 110, and the books balance. These numbers are invented examples, meant to show at a glance that the first group is more than a quarter — and that its root cause is only one thing: a class is not a function, and it cannot be called without new.
Checklist: after writing code with classes, tick each item
First, new makes instances, instances carry data and methods, and the class carries a set of its own; declare a class before using it, and always write new to make an instance. Second, a constructor only hangs data on this and returns nothing; every new makes a new instance. Third, write methods in the class body and all instances share one copy; write them in the constructor and every instance gets a new one. Fourth, mark internal data with #, put checks inside methods, and never hand raw data to the user; methods of one class can read the private fields of other instances of the same class. Fifth, get plus set is a read-write property, get alone is a read-only property; in strict mode, writing to a read-only property throws. Sixth, instance things hang on the instance, class things hang on the class, and instances cannot read statics. Seventh, a subclass calls super first and uses this after; private fields are not inherited, and a class extends only one parent; an overridden method can build on the parent's result with super. Eighth, look for a property on the object itself first, then up the chain; an own property shadows the prototype's; reaching null with nothing found means undefined. Ninth, this inside a method is always the caller, never the place the method lives; writing a property on the child shadows, it does not change the prototype. Tenth, an instance's prototype is the constructor's prototype; mutating the prototype changes all instances together; never re-assign prototype. Eleventh, literals come with prototypes, array methods live on Array.prototype; Constructor.prototype and the constructor's own prototype are not the same thing. Twelfth, set the prototype at creation; an Object.create(null) object does not even have hasOwnProperty; never extend built-in prototypes at will. Thirteenth, the longer the chain, the slower the lookup; check own properties with hasOwnProperty, never by testing for undefined alone. If any item cannot be answered, go back to its section and read it again — that is worth more than memorizing ten more APIs.
Comments (0)
Sources
Related articles
You already call functions every day; this guide names what you have been doing. Following MDN's Functions, Arrow function expressions, and Rest parameters: declarations versus expressions, parameters and arguments, default values, rest parameters, spread syntax, return values, arrow function shorthand, the this trap, callbacks, and scope.
One page should stand up on a phone, a tablet, and a computer. This guide follows MDN's Responsive web design, Using media queries, and the viewport meta reference: fluid HTML, the viewport tag, fluid grids and max-width, images that never burst their container, media query syntax, breakpoints set by content, mobile first, responsive type, testing capability instead of width, and a pre-launch checklist.
Numbers in JavaScript are one type, Number, stored as 64-bit floats — which is why 0.1 + 0.2 is 0.30000000000000004. This guide follows MDN's Basic math in JavaScript, Math, and Number: one number type, float error, counting money in cents, converting strings, NaN and Infinity, safe integers, the four rounding methods, Math.random, the Math toolbox, and formatting for display.