advertisement
How Built-in Objects Work in JavaScript
❌
Built-in Objects
The object model of the JavaScript language forms the foundation of the language. These objects help to provide custom functionalities in the script.
JavaScript treats the primitive data types as objects and provides equivalent objects for each of them. For example, if a variable contains a string of characters, it is treated as a String object in Java Script. Similarly, if a variable contains the value of true or false, it is treated as a Boolean object.
JavaScript objects are categorized as built-in, browser, and HTML.
The built-in objects are static objects that can be used to extend the functionality of the script. Some of these objects are String, Math, and Date. Browser objects, such as window, history, and navigator are used to work with the browser window, whereas HTML objects, such as form, anchor, and so on are used to access elements on a Web page.
1. String Object:
Strings in JavaScript are a set of characters that are surrounded by single or double quotes. These characters can include alphabets, numbers, spaces, and symbols: %, @, &, and so on. The built-in String object allows you to perform different text operations on them. Some examples of these operations include: searching for a specific character occurrence, retrieving a substring, merging two sets of characters, and so on.
The String object is instantiated with the new keyword, which invokes the predefined constructor function of the String object.
The syntax to initialize the String object is as follows:
Syntax
where,
object_ name: Is the instance of the String object
The String object provides different properties and methods to manipulate strings.
Lists the properties of the String object.
- length: Retrieves the number of characters in a string.
- prototype: Adds user-defined properties and methods to the String instance.
List the methods of the String object.
- charAt (): Retrieves a character from a particular position within a string.
- concat (): Merges characters from one string with the characters from another string and retrieves a single new string.
- indexOf (): Retrieves the position at which the specified string value first occurred in the string.
- lastIndex0f (): Retrieves the position at which the specified string value last occurred in the string.
- replace (): Matches a regular expression with the string and replaces it with a new string.
- search (): Searches for a match where the string is in the same format as specified by a regular expression.
- split (): Divides the string into substrings and defines an array of these substrings.
- substring (): Retrieves a part of a string between the specified positions of a string.
- toLowerCase (): Specifies the lowercase display of the string.
- toUpperCase (): Specifies the uppercase display of the string.
The script creates a String object and tests various methods on it.
2. Math Object:
The Math object allows the user to perform mathematical operations on numeric values. The Math object is a pre-defined object that provides static properties and methods to perform mathematical operations. The properties and methods are declared static, thus they can be invoked directly from the object name. In other words, no object instantiation is required for the Math object.
The syntax to access the properties of the Math object is as follows:
Syntax:
where,
PropertyName: Is the name of the property
Similarly, the syntax to invoke the methods of the Math object is as follows:
Syntax:
The script calculates the area of a circle using the Math object.
3. Date Object:
The Date object allows you to define and manipulate date and time values programmatically. It supports both the Universal Time Coordinated (UTC) and Greenwich Mean Time (GMT) conventions. GMT İS the standard time zone that includes Greenwich and divides the globe into 24 zones, each with a difference of an hour in time. UTC splits time into days, hours, minutes, and seconds and approximates GMT.
The Date object calculates dates in milliseconds from 01 January 1970. The date and time can be specified by creating an instance of the Date object.
Various syntaxes to instantiate the Date object are as follows:
Syntax:
i:
To instantiate the Date object with the current date and time as that of the local machine.
var object_name = new Date ();
where,
object name: Is the instance of the Date object.
ii:
To instantiate the Date object by providing the passed milliseconds, since 01 January 1970 as the parameter.
var object_name = new Date (milliseconds);
iii.
To instantiate the Date object by passing date values and optional time values as parameters.
var object_name = new Date (year, month, day, hour, minutes, seconds, milliseconds) ;
Here, the Date object refers to the month numbers from 0 to 11 and treats the first month as 00. Therefore, one needs to specify 02 as the month value for the March month. If the optional arguments are not supplied, they are set to 0.
iv:
To instantiate the Date object by passing date values and optional time values in quotes as the parameters.
var object_name = new Date ("dateString"):
Lists the methods of the Date object.
- getDate (): Retrieves a numeric value between 1 and 31 which indicates the day of the month.
- getDay (): Retrieves a numeric value between 0 and 6 that indicates the day of the week. For example, Sunday is 0, Monday is 1, and so on.
- getTime (): Retrieves a numeric value that indicates the time passed in milliseconds since midnight 01/01/1970.
- getFul11Year (): Retrieves a four-digit numeric value that indicates the year on the given date.
The script displays the current date in the mm/dd/yyyy format.
4. with Statement:
The with statement allows the removal of the object reference for each JavaScript statement. This is done by referring to the common object only once in a set of statements.
The with statement starts with the keyword followed by open and close brackets, which hold statements that refer to a common object. This increases the readability of the code and also reduces the time required in writing each object reference in each related statement.
The syntax to declare a statement is as follows:
Syntax:
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0