UNIT 2 CLIENT SIDE PROGRAMMING
2.6 JavaScript – Built-in Objects
Introduction:
JavaScript provides a variety of built-in objects that offer pre-defined methods and properties to simplify coding tasks, such as working with strings, numbers, dates, and arrays. These objects enhance functionality and make JavaScript programming more efficient.
What are Built-in Objects in JavaScript?
Built-in objects in JavaScript are predefined objects that come with the language to perform common tasks.
Examples of Built-in Objects:
- String
- Number
- Array
- Date
- Math
- Object
String Object:
Represents a sequence of characters and provides methods for string manipulation.
Common Methods:
length: Returns the length of the string.let text = "Hello";console.log(text.length); // Output: 5toUpperCase(): Converts the string to uppercase.console.log(text.toUpperCase()); // Output: HELLOsubstring(start, end): Extracts a portion of the string.console.log(text.substring(1, 4)); // Output: ell
Number Object:
Handles numeric values and provides methods for numerical operations.
Common Methods:
toFixed(digits): Formats a number to a fixed number of decimal places.let num = 5.678;console.log(num.toFixed(2)); // Output: 5.68isNaN(value): Checks if a value is not a number.console.log(isNaN("abc")); // Output: true
Array Object:
Represents a collection of items and provides methods to manipulate arrays.
Common Methods:
push(): Adds an element to the end of the array.let arr = [1, 2, 3];arr.push(4);console.log(arr); // Output: [1, 2, 3, 4]pop(): Removes the last element from the array.arr.pop();console.log(arr); // Output: [1, 2, 3]sort(): Sorts the array.let nums = [3, 1, 4];console.log(nums.sort()); // Output: [1, 3, 4]
Date Object:
Helps work with dates and times.
Common Methods:
new Date(): Creates a new date object.let currentDate = new Date();console.log(currentDate);getFullYear(): Returns the year.console.log(currentDate.getFullYear()); // Output: e.g., 2024getDay(): Returns the day of the week (0 for Sunday).console.log(currentDate.getDay()); // Output: e.g., 5 for Friday
Math Object:
Performs mathematical operations and calculations.
Common Methods:
Math.round(value): Rounds a number to the nearest integer.console.log(Math.round(4.5)); // Output: 5Math.random(): Generates a random number between 0 and 1.console.log(Math.random());Math.max(a, b, ...): Returns the largest value.console.log(Math.max(1, 5, 3)); // Output: 5
Object Object:
The base object from which all other objects are derived.
Common Methods:
Object.keys(obj): Returns the keys of an object.let obj = { name: "John", age: 25 };console.log(Object.keys(obj)); // Output: ["name", "age"]Object.values(obj): Returns the values of an object.console.log(Object.values(obj)); // Output: ["John", 25]
Advantages of Built-in Objects:
- Simplify coding tasks by providing ready-to-use methods.
- Improve code readability and efficiency.
- Reduce the need to write complex algorithms.
Disadvantages of Built-in Objects:
- Overhead: Some built-in objects may carry extra functionality, leading to increased memory usage.
- Complexity for Beginners: Understanding all available methods can be overwhelming for new developers.
- Performance Issues: Improper use of certain methods (e.g., sort() with large arrays) can cause performance bottlenecks.
Real-World Use Cases of Built-in Objects:
- String Object: Form validations and user input handling (e.g., checking the length of a username).
- Number Object: Handling currency, calculations, or formatting numbers in financial applications.
- Array Object: Storing and manipulating data sets, such as user profiles or product lists.
- Date Object: Generating timestamps, managing schedules, or showing the current date in dashboards.
- Math Object: Creating random codes (e.g., OTPs) or performing statistical analysis.
Comments
Post a Comment