Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

Built-in Objects: Choose the Right Tool

Built-in Objects 20 min

javascript / Built-in Objects: Choose the Right Tool

Explore each scene at your own pace.

01 · Built-in Objects: Choose the Right Tool

Built-in Objects: Choose the Right Tool

Built-in Objects: Choose the Right Tool

Built-in objects are standard language facilities such as Object, Array, Math, Date and RegExp. Their APIs solve different problems; they are distinct from browser DOM APIs and Node filesystem APIs.

The language includes a toolbox. Choose a tool by the problem it solves, then inspect the type of result it returns.

Start with the job, not a list of names

A blog receives article records from another system. You need to inspect fields, round a reading time, display a publication date, and check an author handle. Those are four different operations. Creating a page element is a fifth job supplied by the browser, not by a language built-in.

Job Facility Result to inspect
Inspect own enumerable string keys Object.keys(record) An array of key strings
Keep an ordered list Array An array with numeric positions
Round a numeric calculation upward Math.ceil(value) A number
Represent a time instant Date An object whose timestamp can be invalid
Match a text pattern RegExp A match result or boolean through its methods
Convert supported data to text JSON.stringify(value) A string, not an object
Format a value for a locale Intl formatters Display text; not a universal parsing format

Math and JSON are namespaces, so do not write new Math() or new JSON(). Date, Map and Set are commonly constructed with new. Number('4') converts to a primitive number; new Number(4) creates a wrapper object. In particular, new Boolean(false) is an object and therefore truthy. For ordinary application values, prefer primitives and explicit conversions.

Walk through the modelBuilt-in Objects: Choose the Right ToolAuthored snapshots of the example; use Run below to test the code.
recordObject.keys → array
durationMath.ceil → number
publicationDate → timestamp
Match each job to a facility before writing the expression.
1 of 3

Follow the actual results

javascript

Object.keys inspects the record's own enumerable string keys. It does not traverse the prototype chain. Math.ceil(410 / 200) yields 3 because a partial minute rounds upward under this chosen product rule. Rounding is a product decision; it is not proof that 200 words always take a minute to read.

The date string includes Z, which means UTC. toISOString() produces a machine-oriented UTC representation. A display formatter can show that instant differently in another time zone; formatting changes presentation, not the instant. Check Number.isNaN(date.getTime()) before formatting an untrusted date. Keep the ISO instant as data instead of parsing a localized display label back into a date.

The regular expression here validates a deliberately narrow handle contract: start with an ASCII letter; use 3–16 ASCII letters, digits or hyphens in total. It is not a universal name validator, an email validator, or a replacement for server authorization. ^ and $ assert input boundaries when the multiline flag is absent. With the m flag, they can also assert line boundaries. Whole-input handle validation must not accidentally enable multiline matching. The independent handle task checks that boundary.

Predict and repair

Before running, predict Boolean('false') and Boolean(new Boolean(false)). Both are true: the first input is a nonempty string and the second is an object. Repair a checkbox handler by reading its boolean .checked property rather than applying Boolean to its string .value.

Then change the publication string to invalid text. Identify the invalid timestamp before calling toISOString, which would throw. Compare a missing title field with an empty title string: field presence and content validity are different checks.

Independent check

Implement the record summary below without changing its contract. The task checks object-key selection and upward rounding. Separately, explain why a browser document and Node node:fs do not belong in the same category as Math even though all can be called from JavaScript.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.