Ready-made tools for text, numbers, lists, dates and math
JavaScript comes with a toolbox of objects that are already made for you. You don't have to write code to make text all capitals or to round a number. You just call the right tool.
flag
What you'll learn
How to use the ready-made tools for strings, numbers and arrays.
How to do math, and pick something by chance, with Math and Math.random().
How to read today's date with Date, and how Boolean turns values into true or false.
waving_handWelcome to built-in objects
A built-in object is an object that JavaScript gives you, ready to use. You saw objects in JS Lesson 6. These ones hold useful functions, called methods.
Some tools belong to a value, so you write them after it with a dot: "hello".toUpperCase(). Others belong to a big object like Math: Math.round(4.6).
home_repair_service
Think of a toolbox. You don't build a hammer every time you need one. You open the box and pick the right tool.
JavaScript's toolbox has a label maker for text (String), a calculator (Math), a calendar (Date), and even a pair of dice (Math.random()).
text_fieldsLabel makerWorks with text.String.length .toUpperCase()
numbersNumber converterTurns text into numbers.NumberNumber("5") parseInt()
format_list_numberedChecklistAdds, removes and finds items.Array.push() .pop() .join()
calendar_monthCalendarKnows today's date and time.Datenew Date()
calculateCalculatorRounds and finds the biggest.MathMath.round() Math.max()
casinoDicePicks a number by chance.Math.random()
toggle_onLight switchYes or no.BooleanBoolean(0)
Now let's see the built-in objects ↓
text_fieldsLabel makerWorks with text.String.length .toUpperCase()
numbersNumber converterTurns text into numbers.NumberNumber("5") parseInt()
format_list_numberedChecklistAdds, removes and finds items.Array.push() .pop() .join()
calendar_monthCalendarKnows today's date and time.Datenew Date()
calculateCalculatorRounds and finds the biggest.MathMath.round() Math.max()
casinoDicePicks a number by chance.Math.random()
toggle_onLight switchYes or no.BooleanBoolean(0)
Each tool has a name you call, and most need parentheses ( ), just like the functions you wrote in JS Lesson 5.
searchA closer look
Calling a method
headlinethe value
.go inside
toUpperCasemethod (the tool)
()use it now
length is a property, not a method, so it has no parentheses: headline.length. Most other tools in this lesson are methods and need ( ).
Tools give back new values
String tools never change the original text. headline.toUpperCase() gives you a new string, and headline stays the same. To keep the result, save it in a variable.
Array tools are different: push and pop change the array itself.
Text that looks like a number
"49.5" in quotes is a string, so "49.5" + 10 joins them into "49.510". Number("49.5") turns it into a real number first.
This matters later, because anything a visitor types in a text box reaches JavaScript as a string.
Random whole numbers
Math.random() gives a decimal like 0.7342, from 0 up to, but not including, 1.
Multiply it by the number of choices, then use Math.floor() to drop the decimals. Math.floor(Math.random() * 4) gives 0, 1, 2 or 3: a perfect index for an array of 4 items.
Dates count months from 0
getMonth() gives 0 for January and 11 for December, like the index of an array. Add 1 when you show it to people.
getDate(), the day of the month, starts at 1 as usual.
constructionLet's build it
Let's try the tools one box at a time. The HTML and CSS stay the same in every step: a heading and two boxes, #date and #pick.
Most steps write to the console. Steps 5 and 6 also change the page.
1Step 1: String tools
Your code
const headline = "new phones arrive";console.log(headline.length);console.log(headline.toUpperCase());console.log(headline.includes("phones"));console.log(headline.slice(0, 3));
The headline has 17 characters, counting the spaces. toUpperCase() gives NEW PHONES ARRIVE, includes("phones") is true,
and slice(0, 3) takes the characters at index 0, 1 and 2: new.
price is text, so price + 10 joins them into 49.510. Number(price) + 10 really adds: 59.5.
parseInt reads the number at the start of the text: 120. toFixed(2) keeps two decimal places: 3.14.
push adds "Apps" to the end, so the list has 3 items. "Games" is at index 1.
join(" | ") makes one string from the list. Then pop removes the last item.
Math.round goes to the nearest whole number (5), Math.floor always goes down (4), and Math.ceil always goes up (5).
Math.max and Math.min find the biggest and smallest, and Math.sqrt gives the square root.
Line 2 makes a random whole number from 0 to 3, one for each story. The page shows the story at that index.
The pick changes each time the page loads, so reload this lesson and you may get a different story.
6Step 6: Today's date with Date
Your code
const today = new Date();console.log(today.getFullYear());console.log(today.getMonth() + 1);console.log(today.getDate());document.getElementById("date").innerHTML = today.toDateString();
new Date() makes a Date object for right now. Its methods give the year, the month and the day. Notice the + 1 on the month, because months count from 0.
toDateString() gives a short, readable date for the page.
Boolean( ) turns any value into true or false. 0 and empty text "" become false, and most other values become true.
Comparisons, like 10 > 5, already give a boolean.
sticky_note_2Quick notes
StringText tools
"hello".toUpperCase()
How to use
text.lengthHow many characters, spaces included.
text.toUpperCase()All capital letters. toLowerCase() does the opposite.
text.includes("x")true if the text has "x" in it.
text.slice(0, 3)Part of the text: from index 0, up to but not including 3.
text.trim()Removes spaces at both ends.
Examples
Shout a headline.
console.log("breaking news".toUpperCase());
Check a password length.
console.log(password.length >= 8);
NumberNumber tools
Number("49.5") + 10
How to use
Number("42")Changes text into a number.
parseInt("42 kg")Reads the whole number at the start of the text.
parseFloat("3.5")Reads a number with a decimal point.
n.toFixed(2)Shows a number with 2 decimal places, as text.
When text can't be read as a number, you get NaN: Not a Number.
Examples
Add a number typed as text.
const typed = "5";console.log(Number(typed) + 1);
Show money with 2 decimals.
console.log((12.5).toFixed(2));
ArrayList tools
topics.push("Apps");
How to use
list.push(x)Adds x to the end.
list.pop()Removes the last item and gives it back.
list.lengthHow many items.
list.indexOf(x)The index of x, or -1 if it isn't there.
list.join(", ")One string with all the items, joined by ", ".
Examples
Add a new name to a list.
names.push("Dino");
Show a list as one line.
console.log(names.join(", "));
DateDates and times
const today = new Date();
How to use
new Date()The date and time right now.
date.getFullYear()The year, like 2026.
date.getMonth()The month, from 0 (January) to 11 (December).
date.getDate()The day of the month, from 1 to 31.
date.toDateString()A short, readable date, like Thu Oct 08 2026.
Examples
This year.
console.log(new Date().getFullYear());
Show today's date on the page.
document.getElementById("date").innerHTML = new Date().toDateString();
MathMath tools
Math.round(4.6)
How to use
Math.round(4.6)To the nearest whole number: 5.
Math.floor(4.6)Down to a whole number: 4.
Math.ceil(4.2)Up to a whole number: 5.
Math.max(a, b)The biggest. Math.min() gives the smallest.
Math.sqrt(81)The square root: 9.
Examples
Round a grade.
console.log(Math.round(89.5));
The highest score.
console.log(Math.max(85, 92, 78));
Math.random()Random numbers
Math.floor(Math.random() * 4)
How to use
Math.random()A random decimal from 0 up to, but not including, 1.
Math.random() * 6From 0 up to, but not including, 6.
Math.floor(Math.random() * 6)A whole number from 0 to 5.
Math.floor(Math.random() * 6) + 1A whole number from 1 to 6, like a die.
Examples
Roll a die.
const roll = Math.floor(Math.random() * 6) + 1;
Pick a random name.
const i = Math.floor(Math.random() * names.length);console.log(names[i]);
BooleanTrue or false
Boolean("hello")
How to use
Boolean(value)Turns any value into true or false.
0, "", null, undefinedThese become false.
Almost everything else becomes true, like 1, "hi" and [ ].
Examples
Is the box empty?
console.log(Boolean(text));
reportCommon mistakes
These are mistakes many beginners make. Each mini browser shows what really happens.
error
Forgetting the ( ) on a method
The mistake
const headline = "new phones arrive";console.log(headline.toUpperCase);