JavaScript Objects

0

JavaScript Objects




What is an Object in context of JavaScript?
Before reading this tlog JavaScript Objects make sure that you already know about JavaScript (If you’re unaware of JavaScript then click here). In today’s World almost all the modern programming languages are the outcome of so called Object Oriented Concepts. They divide the real World in terms of objects, where an object is nothing but an entity which can be distinguished from other entities. Instances of this concept can also be realized in JavaScripts. Though Java Script’s are not purely object oriented language rather they can be referred to as object based programming language.
Objects in terms of programming disciplines refers to a construct holding data and functions. These are created by using a new operator. Once the script gets executed a separate memory space is reserved for each object, where its (objects) data and functions are stored.

The following are the types of JavaScript Objects

  • Math Object
  • Date Object
  • String Object
  • Array Object

Math Object 

In order to perform several mathematical operations or calculation in JavaScripts, JS has dedicated several mathematical functions. These mathematical functions or methods are entities of the JavaScript Objects “Math” most commonly “Math Object”. To utilize these functions in our script we need to follow the following syntax.

Syntax : Math.method(numeric value);

Math Object Methods

Method Returns
abs(x) the absolute value of x
acos(x) the arccosine of x, in radians
asin(x) the arcsine of x, in radians
atan(x) the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x) the arctangent of the quotient of its arguments
ceil(x) x, rounded upwards to the nearest integer
cos(x) the cosine of x (x is in radians)
exp(x) the value of Ex
floor(x) x, rounded downwards to the nearest integer
log(x) the natural logarithm (base E) of x
max(x,y,z,…,n) the number with the highest value
min(x,y,z,…,n) the number with the lowest value
pow(x,y) the value of x to the power of y
random() a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) the sine of x (x is in radians)
sqrt(x) the square root of x
tan(x) the tangent of an angle




Date Objects :

Date forms one of the important specifications in almost every programming discIpllne. Hence JavaScripts has reserved a separate date class in this aspect. This data class includes variety of methods to access the date.
Globally date/time is recognized in two ways (i.e.,) UTC (Universal Time or Greenwich Mean Time) and local time,
Universal time as the name suggests is a standard followed through out the world. On the other hand local time is the time of the system where the script is currently residing.
There are 4 ways in order to get the date. They are

  • new Date()
  • new Date(dateString)
  • new Date(milliseconds)
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)

Example Snippet :
<!DOCTYPE html>
<html>
<body>
<script>
var present_date = new Date();
document.writeln(present_date);
</script>
</body>
</html>

Output : Wed Jan 13 2016 19:23:05 GMT+0530 (India Standard Time)

Date Get Methods :

Method Description
getDate() Get the day as a number (1-31)
getDay() Get the weekday as a number (0-6)
getFullYear() Get the four digit year (yyyy)
getHours() Get the hour (0-23)
getMilliseconds() Get the milliseconds (0-999)
getMinutes() Get the minutes (0-59)
getMonth() Get the month (0-11)
getSeconds() Get the seconds (0-59)
getTime() Get the time (milliseconds since January 1, 1970)

Date Set Methods :

Method Description
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)

String Object : 

String function forms one of the important consequence in JavaScript. In general a string refers to set of characters enclosed under double quotes. Like math even strings are treated as objects in JS and they support various methods displayed below

String Methods

Here is a list of the methods available in String object along with their description.

Method Returns
charAt() the character at the specified index.
charCodeAt() a number indicating the Unicode value of the character at the given index.
concat() Combines the text of two strings and a new string.
indexOf() the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
lastIndexOf() the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
localeCompare() a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
match() Used to match a regular expression against a string.
replace() Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
search() Executes the search for a match between a regular expression and a specified string.
slice() Extracts a section of a string and a new string.
split() Splits a String object into an array of strings by separating the string into substrings.
substr() the characters in a string beginning at the specified location through the specified number of characters.
substring() the characters in a string between two indexes into the string.
toLocaleLowerCase() The characters within a string are converted to lower case while respecting the current locale.
toLocaleUpperCase() The characters within a string are converted to upper case while respecting the current locale.
toLowerCase() the calling string value converted to lower case.
toString() a string representing the specified object.
toUpperCase() the calling string value converted to uppercase.
valueOf() the primitive value of the specified object.

Array Objects : 

The following are the methods supported by JavaScript for Arrays.

Array Methods

Here is a list of the methods of the Array object along with their description.

Method Description
concat() Returns a new array comprised of this array joined with other array(s) and/or value(s).
every() Returns true if every element in this array satisfies the provided testing function.
filter() Creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach() Calls a function for each element in the array.
indexOf() Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
join() Joins all elements of an array into a string.
lastIndexOf() Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
map() Creates a new array with the results of calling a provided function on every element in this array.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements to the end of an array and returns the new length of the array.
reduce() Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight() Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
reverse() Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.
shift() Removes the first element from an array and returns that element.
slice() Extracts a section of an array and returns a new array.
some() Returns true if at least one element in this array satisfies the provided testing function.
toSource() Represents the source code of an object
sort() Represents the source code of an object
splice() Adds and/or removes elements from an array.
toString() Returns a string representing the array and its elements.
unshift() Adds one or more elements to the front of an array and returns the new length of the array.

 

Related topics

Note : For easy reference the tables used in this TLOG are taken from w3schools and tutorialspoint.

Share.

Leave A Reply