To add flexibility in language, JavaScript supports a number of built-in objects. The most commonly used objects are:
Implementations of these built-in objects are complex and different.
Global objects are objects defined outside the function. Every function can access these variables because their scope is global for all.
When you do not declare a variable and assign a value to it, then it will automatically become global.
When your code starts execution, functions and constants immediately become available. A global variable does not initialize with the new
keyword. Basically, global objects are used to share same data to add properties. You can store methods within global objects in your script.
Objects that you cannot access directly for accessing those object we use global objects. We pass those object directly as arguments after declaring the global variable. You can create a number of instances and a number of global objects in your script.
A global object has a fixed number of properties. Multiple object instances can access this global object. Here is an example:
varstudentName="Ali" functionmyStd(){ } student=new Global("name"); student.age=18; person=new Global("name"); post(person.age);
Date objects in JavaScript deal with date and time objects. For example, if we are writing a script and we need some functionality of date and time, then we can use this built-in object simply.
This function is used to get the current time with respect to the number of milliseconds since January 1, 1970.
Methods are available in date objects to manipulate dates. We can also adjust the date dynamically. Here is an example:
var dob=new date(); dob.setDate(19.03.1990);
We can also set upcoming dates and the current date by using this function, for example:
var dob=new date(); dob.setDate(dob.getDate + 7);
The date set methods are:
setTime()
setMilliseconds()
setMinutes()
setMinutes()
setHours()
setDate()
setDay()
setMonth()
setFullYear()
These methods are very similar to the date get methods described in the preceding term list.
We can also compare dates using the date object.
In JavaScript, the math object is used to perform mathematical operations.
This object has several mathematical functions. Here is an example:
Math.E
Math.PI
Math.sqrt
Math.Ln2
Math.ln10
The math
object has different methods. For example, we have the pow
method that calculates the power of the first variable times the second.
Math.pow(base, exponent); document.write(Math.pow(2,4)); // 16 here 2 is base and 4 is exponent.
In JavaScript, for pattern matches in string, we use a regular expression. It is a very powerful and useful tool for expression pattern matching.
The following are the parameters:
g
: Global matchi
: Ignore casem
: Multiline; treat beginning and end characters (^
and $
) as working over multiple lines (that is, match the beginning or end of each line (delimited by
or
), not only the very beginning or end of the whole input string)Using a regular expression, you can make a complex task simple by writing few lines of codes. There are five methods in JavaScript:
RegExp.exec(pattern)
RegExp.replace(pattern)
RegExp.split(pattern)
RegExp.match(pattern)
There are two ways of writing a regular expression in JavaScript. These are:
This method is used to dynamically construct a string search pattern. A regular expression in this method should be written in quotation marks. This method has three parameters. Here is an example:
var email=new RegExp("d{2},"g");
In this example:
d
is a pattern parameter used to match regular expressiong
is global, which is a flag parameter. In this function, there are four types of parameters (g
, I
, m
,u
).In literal notation, we write a regular expression without brackets. Here, i
is a flag that shows to ignore case of the text whether it is uppercase, lowercase or any other. We have more flag objects such as the following ones:
g
: Global objecti
: Ignore casem
: Multi searchu
: Unicode searchIn regular expressions, we can have a global regular expression object, which will have information for each match case. A simple regular expression object has information about a particular regular expression:
var exp=d{2}/i
There are four string methods for pattern matching. In these objects, a pattern is sent with a parameter. These methods allow you to search, match, replace, and split patterns.
These methods are represented as follows.
This method is used to find a matching pattern within a string. Using the not
(!
) operator it can also be used to find non-matches.
The syntax is string.Match(Expression)
.
This runs a search for matching strings in a regular expression. If the search is matched or successful then it will return an array of matching results, if not then it will return
null
or 0
. It is also used to update properties in a regular expression.
Here is an example:
var str=("I have 10 dollars"); //The pattern below is used to find non-digits in a string var parsestring= str.match(/D/g); // Outputs I, ,h,a,v,e, , ,D,o,l,l,a,r,s
This method is used to replace a part of the string.
The syntax is string.replace(stringSearched, stringReplacement)
.
This executes a search and is used for replacement of regular expression match resulted with alternative text. This is also used for replacement of regular expressions with specific and different regExp
properties.
Here is an example:
var str1=("Apple Pie"); var parsestring1=str1.replace("Pie", "Cinnamon Roll");
Here the value of parsestring1
is Apple Cinnamon Roll
.
Also, only the first occurrence of the pattern is replaced. Hence:
var str1=("Apple Pie , Banana Pie", "Strawberry PIE"); var parsestring1=str1.replace("Pie", "Cinnamon Roll");
Here the value of
parsestring1
is Apple Cinnamon Roll
, Banana Pie
, Strawberry PIE
.
To perform a global search and replacement so that all occurrences are replaced we use the following code:
var str1=("Apple Pie , Banana Pie", "Strawberry PIE"); var parsestring1=str1.replace(/Pie/g, "Cinnamon Roll");
Here the value of parsestring1
is Apple Cinnamon Roll
, Banana Cinnamon Roll
, Strawberry PIE
.
For a global insensitive search, use the following code:
var str1=("Apple Pie , Banana Pie, "Strawberry PIE"); var parsestring1=str1.replace(/Pie/gi, "Cinnamon Roll");
Here the value of parsestring1
is Apple Cinnamon Roll
, Banana Cinnamon Roll
, Strawberry Cinnamon Roll
.
This is use to split a string in a regular expression.
The syntax is string.split(separator,limit)
.
With this method the string is split. Every word in the string is treated as a single element in an array. If a null string is passed as a parameter, the method causes each letter to be split into different characters. For example:
var str1 = "My Car is at the garage in Queens"; var parseString = str1.split("");
The value of parseString
will be:
M,y, ,C,a,r, ,i,s, ,a,t, ,t,h,e, ,g,a,r,a,g,e, ,i,n, ,Q,u,e,e,n,s
Providing a limit will return a comma separated array of the words containing only the specified number of elements. For example:
var str1 = "My Car is at the garage in Queens"; var parseString = str1.split(" ", 3);
The value of parseString
is My,Car,is
.
Using a letter or letters as a separator will give the following results:
var str1 = "My Car is at the garage in Queens"; var parseString = str1.split("a");
The value of parseString
will be:
My C,r is ,t the g,r,ge in Queens
Here is another example:
var str1 = "My Car is at the garage in Queens"; var parseString = str1.split("ar");
The value of parseString
will be:
My C, is at the g,age in Queens
This method is used to search for a particular string.
The syntax is string.search(stringSearched)
.
It returns the position of the starting letter of the string if a match is found. If no match is found then it returns -1
.
It is used to find a match in a string. If a match is found it will send an index of that match otherwise, if a match is not found, it will return -1
. Global flags are not supported by this method.
Here is an example:
var str1=("I have 10 dollars in my pocket"); var parsestring=str1.search("i");
The value of parseString
here is 18
.
There are additional string methods available to perform other tasks on strings.
A detailed list of these methods can be found at https://msdn.microsoft.com/en-us/library/ecczf11c(v=vs.94).aspx.
An array is a collection of objects.
To create an array in JavaScript, the elements of the collection are enclosed within square brackets and separated by commas, as seen here:
varcolors = ["red", "yellow", "blue"]
An array can also be initialized using the new
keyword or by specifying their length between 0 and 232-1:
new array(first, second, third, … ) new array(7) // creates an array of size 7
To access array elements, we can use an index notation:
varlastColor = colors[2]; // blue
Always remember that array elements always start with the zero index. Hence, the third element in the preceding array has the index of 2
.
Array objects can hold all sorts of data, for example, strings, numbers, literals, dates, and even user-defined objects.
This method is used to pop an element out of an array.
Here an array is considered as a stack and the elements that are last in the array are popped out first. This follows the LIFO principle.
Here is an example:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var element= sweets.Pop();
This method is used to push an element into an array.
Here an array is considered as a stack and the elements that are pushed appear at the end of the array.
Here is an example:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var element= sweets.Push("Lemon Meringue");
The value of the element here is 6
.
This converts the elements in an array into a string. The elements appear as comma-separated strings.
The ToString()
function is used to convert an array into a string. The elements of the array appear in the string and are separated by a comma:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.ToString()
Here the value of StrSweets
is:
Red Velvet,Chocolate Mousse,Strawberry Delight,Pineapple Sundae,Black Forest
This method is also used to convert an array into a string.
This is default behaviour of the array and works same as the ToString()
function.
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.ValueOf()
Here the value of StrSweets
is:
Red Velvet,Chocolate Mousse,Strawberry Delight,Pineapple Sundae,Black Forest
This method is used to convert an array into a string. The elements are separated by the delimiter specified.
This returns a string containing the elements of the array separated by the delimiter specified.
The Join()
function is used to convert an array into a string. The elements of the array appear in the string and are separated by the delimter:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.Join(*)
Here the value of StrSweets
is:
Red Velvet * Chocolate Mousse * Strawberry Delight * Pineapple Sundae * Black Forest
Splice, as the name suggests, is used to add new elements into the array. Unlike the push()
method we can add elements in whichever position we want.
This method is used to remove and add elements in one step. We can specify where the new element is to be added and which elements are to be removed.
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.splice(2, 0, "Lemon Meringue");
Here the value of StrSweets
is:
Red Velvet,Chocolate Mousse,Lemon Meringue,Strawberry Delight,Pineapple Sundae,Black Forest
This method is used to sort an array into alphabetical order.
The sort method is used to sort an array, alphabetically.
Here is an example:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.Sort();
Here the value of StrSweets
is:
Black Forest,Chocolate Mousse,Pineapple Sundae,Red Velvet,Strawberry Delight
As the name suggests it reverses the order of the array.
The reverse method is used to sort an array, alphabetically, in reverse order.
Here is an example:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.reverse();
Here the value of StrSweets
is:
Strawberry Delight,Red Velvet,Pineapple Sundae,Chocolate Mousse,Black Forest.slice()
As the name suggests, this method is used to slice up an array and create a new array using a part of the original array.
The index at which we want the array sliced. The index of an array begins at 0
. So the first element has index 0
, the second element has index 1
, and so on.
The slice method is used to slice up an array and get the remainder of the array as a string.
Here is an example:
var sweets = ["Red Velvet", "Chocolate Mousse", "Strawberry Delight", "Pineapple Sundae", "Black Forest"]; var StrSweets= sweets.slice(3);
Here the value of StrSweets
is:
Pineapple Sundae,Black Forest
This method is used to concatenate two or more arrays into a single array.
The Concat()
method is used to create a single array by joining two or more arrays.
Here is an example:
var greenShades= ["Mint", "Basil", "Pine", "Emerald"]; var BlueShades=["Azure", "Cerulean", "Navy", "Aegan"]; var VioletShades= ["Lilac", "Orchid", "Mauve", "Wisteria"]; var CoolShades=greenShades.concat(BlueShades, VioletShades);
Here the value of CoolShades
is:
Mint,Basil,Pine,Emerald,Azure,Cerulean,Navy,Aegan,Lilac,Orchid,Mauve,Wisteria
3.145.166.149