Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Functions  ›  Capstone Project

Built-in Functions

Built-in Functions 10 minJavaScript ships a toolbox of global and Math functions ready in every file with no import: rounding and random numbers on Math, number parsing with parseInt/parseFloat, and type checks like isNaN and Number.isInteger — each with an edge case worth knowing.

Built-in Functions

In short: JavaScript ships with a toolbox of functions available in every file, with no import needed — Math.round, Math.max, parseInt, isNaN, Number.isInteger, and many more. They cover rounding, comparing, random numbers, and safely checking a value's shape. Most behave exactly as the name suggests, but a few have a sharp edge worth memorising, because they don't fail loudly when misused.

A toolbox of ready-made calls: Math.round(4.7)→5, Math.max(3,9,1)→9, parseInt("42px")→42, isNaN("abc"*2)→true, Number.isInteger(4)→true, Math.random()→0..1 — a note that none of these need an import, and a trap that Math.max() with no args is -Infinity

Rounding and comparing without writing your own logic

Math bundles the everyday numeric helpers:

javascript

Math.round rounds to the nearest whole number, Math.max/Math.min take any number of arguments and return the largest/smallest. No loop, no manual comparison — the toolbox already has these.

Reading numbers out of messy text

parseInt and parseFloat, from the type-casting lessons, live here too — global functions, not tied to any object:

javascript

parseInt/parseFloat read the leading numeric part and stop at the first character that doesn't fit. isNaN checks whether a value, after being coerced to a number, comes out as NaN — "abc" * 2 already is NaN, so this is true. Number.isInteger checks for a genuine whole number, no coercion involved.

Two traps: empty Math.max(), and isNaN's coercion

Both functions have a documented, surprising edge case:

javascript

Math.max() with nothing to compare returns -Infinity (anything beats "nothing"), and Math.min() returns Infinity for the same reason — surprising if you expected 0 or an error. isNaN("hello") is true because the global version coerces "hello" to a number first — which is NaN — before checking. Number.isNaN("hello") does no coercion at all, so a string is never NaN itself, and it correctly says false. Prefer Number.isNaN when you want to know "is this value literally NaN," not "would this coerce to NaN."

Quick practice

quick drill

What does Math.max(3, 9, 1) return?

quick drill

What does Math.max() return, with no arguments at all?

quick drill

Why does isNaN("hello") return true while Number.isNaN("hello") returns false?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainisNaN vs Number.isNaN

isNaN("hello") is true, but Number.isNaN("hello") is false. Explain why these two built-ins disagree on the same input.