Built-in Objects and Methods
JavaScript comes with several built-in
objects that provide ready-to-use properties and methods for common
tasks like working with strings, numbers, dates, and more.
1. String Object
Strings represent sequences of characters. The String
object provides many helpful methods.
let text = "Hello, JavaScript!"; console.log(text.length); // 18console.log(text.toUpperCase()); // HELLO, JAVASCRIPT!console.log(text.toLowerCase()); // hello, javascript!console.log(text.includes("Java")); // trueconsole.log(text.indexOf("Java")); // 7console.log(text.slice(7, 17)); // JavaScript
2. Number Object
The Number object provides constants and
methods for working with numeric values.
let num = 42.567; console.log(num.toFixed(2)); // "42.57" (round to 2 decimal places)console.log(Number.isInteger(num)); // falseconsole.log(Number.parseInt("123px")); // 123console.log(Number.parseFloat("3.14 meters")); // 3.14
3. Math Object
The Math object contains properties and
methods for mathematical calculations.
console.log(Math.PI); // 3.141592653589793console.log(Math.round(4.7)); // 5console.log(Math.floor(4.7)); // 4console.log(Math.ceil(4.1)); // 5console.log(Math.max(10, 20, 30)); // 30console.log(Math.random()); // Random number between 0 and 1
4. Date Object
The Date object is used to work with dates
and times.
let now = new Date(); console.log(now); // Current date and timeconsole.log(now.getFullYear()); // Yearconsole.log(now.getMonth() + 1); // Month (0-based, so add 1)console.log(now.getDate()); // Day of the monthconsole.log(now.toDateString()); // Human-readable date
5. Array Object (Extra)
Arrays are also built-in objects with powerful
methods.
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits.join(", ")); // "Apple, Banana, Cherry"console.log(fruits.includes("Banana")); // trueconsole.log(fruits.reverse()); // ["Cherry", "Banana", "Apple"]console.log(fruits.sort()); // ["Apple", "Banana", "Cherry"]