JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly.

Syntax to add property to add object

objectName.objectProperty = propertyValue;

We use the write() method to document objects to write any content on the document.

Document.write("Hello world")

Example

The following example shows how to create an Object.

<!DOCTYPE html>  
<html>  
<head>  
<title>User-defined objects</title>  
<script type="text/javascript">  
var book = new Object();   // Object created  
book.subject = "5 points someone"; // Properties assign to the object  
book.author  = "ChetanBhagat";  
</script>  
</head>  
<body>  
<script type="text/javascript">  
document.write("Book Name is : " + book.subject + "<br>");  
document.write("Book Author is : " + book.author + "<br>");  
</script>  
</body>  
</html>

Try Now

Output

Book Name is: 5 points someone
Book Author is: Chetan Bhagat

Methods of an Object

Example

<!DOCTYPE html>  
<html>  
<head>  
<title>User-defined objects</title>  
<script type="text/javascript">// Define a function which will work as a method  
function addPrice(amount){  
this.price = amount;   
}  
function book(title, author){  
this.itle = title;  
this.author  = author;  
this.addPrice = addPrice; // Assign that method as property.  
}  
</script>  
</head>  
<body>  
<script type="text/javascript">  
varmyBook = new book("5 Points Someone", "ChetanBhagat");  
myBook.addPrice(150);  
document.write("Book Title is : " + myBook.title + "<br>");  
document.write("Book Author is : " + myBook.author + "<br>");  
document.write("Book Price is : " + myBook.price + "<br>");  
</script>  
</body>  
</html>

Try Now

Output

Book Title is: 5 Points Someone
Book Author is :ChetanBhagat
Book Price is : 150