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 Name | Description |
---|---|
Object | Represents a generic object in JavaScript |
Array | Used to create and manipulate arrays in JavaScript |
Math | Provides a set of mathematical functions and constants |
Date | Used to work with dates and times in JavaScript |
String | Provides a set of methods for working with strings in JavaScript |
RegExp | Used to work with regular expressions in JavaScript |
Function | Used to create new functions in JavaScript |
Number | Provides a set of methods and properties for working with numbers in JavaScript |
Boolean | Represents a Boolean value, which can be either true or false |
Error | Used to represent and handle errors in JavaScript |
JSON | Provides 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:
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