Objects:
1)An object is a collection of key value pairs.(or)2)An object is a unordered list of datatypes that is stored as name-value pairs.
3)Booleans,Numbers,Strings can be objects.
4)Dates,Maths,Regular Expressions,Arrays,Functions also objects.
5)JS objects are mutable.
Syntax:
var obj = { }; (or)
var obj = new Object();
Assigning values:
obj.name=".com";
obj.name1=".net";
obj.name2=".gov";
(or)
var obj = {
name=".com";
name1=".net";
name2=".gov";
Accessing Object value:
document.writeln("name1: "+obj.name1); (or)
document.writeln("name1: "+obj['name1']);
Deleting property:
The delete statement is used to delete property in object.
delete obj.name;
Task: Take computer as object and display all its properties
Code:
Output:
Object Methods:
In JS, methods are actions performed on objects.
The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object.
Code:
Output:
Date Object in JS:
JavaScript is use to set the current date/time in different different format (like milliSecond, minutes, hours etc).
Date Objects are created with new Date() constructor.
Code:
Output:
Date Formats:
Code:
Output:
Date Methods:
By using date methods, we can get and set values to Date Object.
Get Methods:
getDate(): Get the Date of the Month.
getDay(): Get the Day of the Week.
getFullYear(): Get the Full Year in four Digit.
getHours(): Get the Hours between 0 to 23.
getMilliseconds(): Get the Milliseconds between 0 to 999.
getMinutes(): Get the Minutes between 0 to 59.
getMonth(): Get the Month between 0 to 11.
getSeconds(): Get the Seconds between 0 to 59.
getTime(): Get the Milliseconds since Jan 1, 1970.
Set Methods:
setDate(): Set the Date of the Month.
setFullYear(): Set the Full Year in four Digit.
setHours(): Set the Hours between 0 to 23.
setMilliseconds(): Set the Milliseconds between 0 to 999.
setMinutes(): Set the Minutes between 0 to 59.
setMonth(): Set the Month between 0 to 11.
setSeconds(): Set the Seconds between 0 to 59.
setTime(): Set the Milliseconds since Jan 1, 1970.
Code:
Output:
Math Object:
In JS, Math object is used to perform mathematical operations.
Methods:
abs(x): returns absolute value.
ceil(x): returns the value that is rounded to nearest integer.
exp(x): returns exponential value.
floor(x): returns the value that is rounded down to nearest integer.
round(x): returns the value that is rounded to nearest integer.
log(x): returns logarithmic value.
max(x1, x2, x3..x4): returns maximum value among 'n' numbers.
min(x1, x2, x3..x4): returns minimum value among 'n' numbers.
pow(x,y): returns x power y.
random(): returns number between 0 to 1.
Code:
Output: