Skip to content

Built-in objects in JavaScript

  • by

JavaScript has many built-in objects that provide functionality for common tasks. Here are some of the most commonly used built-in objects in JavaScript:

Object NameDescription
ObjectRepresents a generic object in JavaScript
ArrayUsed to create and manipulate arrays in JavaScript
MathProvides a set of mathematical functions and constants
DateUsed to work with dates and times in JavaScript
StringProvides a set of methods for working with strings in JavaScript
RegExpUsed to work with regular expressions in JavaScript
FunctionUsed to create new functions in JavaScript
NumberProvides a set of methods and properties for working with numbers in JavaScript
BooleanRepresents a Boolean value, which can be either true or false
ErrorUsed to represent and handle errors in JavaScript
JSONProvides methods for working with JavaScript Object Notation (JSON)

Each of these objects has its own set of methods and properties that can be used to perform various operations. For example, the Array object has methods like push() and pop() for adding and removing elements from an array.

While the Math object has methods like round() and sqrt() for rounding numbers and finding square roots, respectively.

Similarly, the Date object has methods like getDate() and getMonth() for getting the date and month of a given date, while the String object has methods like toUpperCase() and toLowerCase() for changing the case of a string.

Built-in objects in JavaScript examples

Simple example code of how to use the built-in objects in JavaScript:

<!DOCTYPE html>
<html>
<body>
    <script>
        // object
        let person = new Object();
        person.firstName = "John";
        person.lastName = "Doe";
        console.log(person);

        // array
        let fruits = ["apple", "banana", "orange"];
        console.log(fruits);

        // math 
        console.log(Math.round(3.5));
        console.log(Math.pow(2, 3));

        // date
        let today = new Date();
        console.log(today.toDateString());

        // string
        let message = "Hello, world!";
        console.log(message.toUpperCase()); 

        // RegExp 
        let pattern = /[A-Za-z]+/;
        console.log(pattern.test("Hello"));
        console.log(pattern.test("123")); 

        // function 
        let sum = new Function("a", "b", "return a + b;");
        console.log(sum(2, 3)); 

        // number
        let x = 10;
        console.log(x)
        console.log(Number.MAX_VALUE); 

        // Boolean
        let a = true;
        let b = false;
        console.log(Boolean(a, b));

        // error
        try {
            throw new Error("Something went wrong");
        } catch (e) {
            console.log(e.message);
        }

        // json
        let data = '{"name":"John","age":30,"city":"New York"}';
        let p = JSON.parse(data);
        console.log(p)
    </script>
</body>
</html>

Output:

Built-in objects in JavaScript

Do comment if you have any doubts or suggestions on this Js object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *