×

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

Related Topics

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript Cookies

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of...

1 minute read.

Javascript Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 minutes read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.