JS · Lesson 7

Built-in Objects

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.

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.

Welcome 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).

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()).

Each tool has a name you call, and most need parentheses ( ), just like the functions you wrote in JS Lesson 5.

A closer look

Calling a method

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.

Let'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.

Step 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));
What you see

Console

    Nothing here yet. console.log() writes here.

    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.

    Step 2: Number tools

    Your code
    const price = "49.5";console.log(price + 10);console.log(Number(price) + 10);console.log(parseInt("120 readers"));console.log((3.14159).toFixed(2));
    What you see

    Console

      Nothing here yet. console.log() writes here.

      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.

      Step 3: Array tools

      Your code
      const topics = ["Phones", "Games"];topics.push("Apps");console.log(topics);console.log(topics.length);console.log(topics.indexOf("Games"));console.log(topics.join(" | "));topics.pop();console.log(topics);
      What you see

      Console

        Nothing here yet. console.log() writes here.

        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.

        Step 4: Math tools

        Your code
        console.log(Math.round(4.6));console.log(Math.floor(4.6));console.log(Math.ceil(4.2));console.log(Math.max(12, 40, 7));console.log(Math.min(12, 40, 7));console.log(Math.sqrt(81));
        What you see

        Console

          Nothing here yet. console.log() writes here.

          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.

          Step 5: Pick by chance with Math.random()

          Your code
          const stories = ["Phones", "Games", "Apps", "Robots"];const index = Math.floor(Math.random() * stories.length);console.log(index);document.getElementById("pick").innerHTML = "Today's pick: " + stories[index];
          What you see

          Console

            Nothing here yet. console.log() writes here.

            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.

            Step 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();
            What you see

            Console

              Nothing here yet. console.log() writes here.

              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.

              Step 7: True or false with Boolean

              Your code
              console.log(Boolean(1));console.log(Boolean(0));console.log(Boolean("hello"));console.log(Boolean(""));console.log(10 > 5);
              What you see

              Console

                Nothing here yet. console.log() writes here.

                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.

                Quick notes

                String Text tools
                "hello".toUpperCase()

                How to use

                • text.length How 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

                1. Shout a headline.

                  console.log("breaking news".toUpperCase());
                2. Check a password length.

                  console.log(password.length >= 8);
                Number Number 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

                1. Add a number typed as text.

                  const typed = "5";console.log(Number(typed) + 1);
                2. Show money with 2 decimals.

                  console.log((12.5).toFixed(2));
                Array List 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.length How 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

                1. Add a new name to a list.

                  names.push("Dino");
                2. Show a list as one line.

                  console.log(names.join(", "));
                Date Dates 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

                1. This year.

                  console.log(new Date().getFullYear());
                2. Show today's date on the page.

                  document.getElementById("date").innerHTML =  new Date().toDateString();
                Math Math 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

                1. Round a grade.

                  console.log(Math.round(89.5));
                2. 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() * 6 From 0 up to, but not including, 6.
                • Math.floor(Math.random() * 6) A whole number from 0 to 5.
                • Math.floor(Math.random() * 6) + 1 A whole number from 1 to 6, like a die.

                Examples

                1. Roll a die.

                  const roll = Math.floor(Math.random() * 6) + 1;
                2. Pick a random name.

                  const i = Math.floor(Math.random() * names.length);console.log(names[i]);
                Boolean True or false
                Boolean("hello")

                How to use

                • Boolean(value) Turns any value into true or false.
                • 0, "", null, undefined These become false.
                • Almost everything else becomes true, like 1, "hi" and [ ].

                Examples

                1. Is the box empty?

                  console.log(Boolean(text));

                Common mistakes

                These are mistakes many beginners make. Each mini browser shows what really happens.

                Forgetting the ( ) on a method

                The mistake
                const headline = "new phones arrive";console.log(headline.toUpperCase);
                What you see

                Console

                  Nothing here yet. console.log() writes here.

                  Without ( ), toUpperCase isn't used. You get the tool itself instead of its result, so the console shows a function, not capital letters.

                  Tip: Add ( ) to use a method: headline.toUpperCase().

                  Using Math.random() without Math.floor()

                  The mistake
                  const stories = ["Phones", "Games", "Apps", "Robots"];const index = Math.random() * stories.length;console.log(index);document.getElementById("pick").innerHTML = "Today's pick: " + stories[index];
                  What you see

                  Console

                    Nothing here yet. console.log() writes here.

                    The index has decimals, like 2.718. There's no item at index 2.718, so the page shows undefined.

                    Tip: Wrap it in Math.floor( ) to get a whole number you can use as an index.

                    Forgetting that months start at 0

                    The mistake
                    const day = new Date(2026, 9, 8);console.log("Month:", day.getMonth());console.log(day.toDateString());
                    What you see

                    Console

                      Nothing here yet. console.log() writes here.

                      new Date(2026, 9, 8) is October 8, not September, because 9 means the tenth month. And getMonth() gives 9 for October.

                      Tip: Add 1 to getMonth() before showing it, and use 9 for October when you make a date.

                      Test your knowledge

                      Write JavaScript in the JS tab. The names array is ready for you. Pick a lucky winner, show today's date, and try a few other tools along the way.

                      No clicking needed. Each goal gets a check mark by itself once your code does it.

                      1. Use a String tool, like .toUpperCase() or .length. not done yet
                      2. Use an Array tool, like .push() or .join(). not done yet
                      3. Pick a random name with Math.random(), and show it in #winner. not done yet
                      4. Show today's date in #today with new Date(). not done yet
                      Your code
                      What you see

                      Console

                        Nothing here yet. console.log() writes here.

                        Try an exercise

                        Recap

                        • Strings, numbers and arrays have their own tools, like .toUpperCase(), Number(), .push() and .join().
                        • Math rounds and compares numbers, and Math.floor(Math.random() * n) picks a whole number from 0 to n - 1.
                        • new Date() gives the date and time now, with months counted from 0. Boolean() turns any value into true or false.

                        References

                        Lesson content prepared by the WebCore F.E. team for beginner learners. The following references were used in preparing this lesson.

                        • ECMA International. ECMAScript Language Specification (official specification). Open reference