Built-in Objects in JavaScript
JavaScript provides several built-in objects that offer a wide range of functionality and can be used to perform various tasks. These built-in objects serve as containers for methods and properties that allow you to perform operations related to specific concepts or data types. In this article, we'll cover some of the most commonly used built-in objects in JavaScript.
Let's look at each of these and understand it with the help of an example.
Math
The Math object provides mathematical operations and constants. It includes methods like Math.random() for generating random numbers, Math.floor() for rounding down a number to the nearest integer, Math.max() and Math.min() for finding the maximum and minimum values in a set of numbers, and more.
Example:
let randomNumber = Math.random();
let roundedNumber = Math.floor(3.7);
let maxNumber = Math.max(5, 2, 8, 3);
Date
The Date object represents dates and times in JavaScript. It allows you to create and manipulate dates, perform operations like getting the current date and time, formatting dates, and extracting specific components such as the year, month, day, hour, minute, and second.
Example:
let currentDate = new Date<span>(</span><span>)</span>;
let currentYear = currentDate.getFullYear();
let currentMonth = currentDate.getMonth();
let currentDay = currentDate.getDate();
Array
The Array object represents an ordered list of values and provides various methods for working with arrays. It includes methods like push() and pop() for adding and removing elements at the end of an array, shift() and unshift() for adding and removing elements at the beginning of an array, concat() for merging arrays, slice() for creating a shallow copy of a portion of an array, and many more.
Example:
let numbers = [1, 2, 3];
numbers.push(4);
numbers.pop();
numbers.shift();
numbers.unshift(0);
let mergedArray = numbers.concat([5, 6]);
let slicedArray = numbers.slice(1, 3);
String
The String object represents a sequence of characters and provides various methods for manipulating and working with strings. It includes methods like charAt() to get the character at a specific position, substring() to extract a portion of a string, toUpperCase() and toLowerCase() to convert the string's case, split() to split a string into an array of substrings, and many more.
Example:
let message = 'Hello, World!';
let character = message.charAt(0);
let subString = message.substring(0, 5);
let upperCaseMessage = message.toUpperCase();
let words = message.split(', ');
JSON
The JSON object provides methods for parsing JavaScript Object Notation (JSON) strings and converting objects to JSON strings. It includes methods like JSON.stringify() to convert an object to a JSON string, and JSON.parse() to parse a JSON string and create an object.
Example:
let person = { name: 'John', age: 30 };
let jsonString = JSON.stringify(person);
let parsedObject = JSON.parse(jsonString);
Conclusion
These are just a few examples of the many built-in objects in JavaScript. Each object serves a specific purpose and offers a set of methods and properties to perform related operations. Understanding and utilizing these built-in objects allows you to leverage powerful functionality and simplify your code when working with specific concepts or data types in JavaScript.'