

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create objects in JavaScript?
To create objects, the new operator is used to create an instance of an object. The new operator is followed by the constructor method.
A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable.
The variable contains a reference to the new object. The properties assigned to the object are not variables and are not defined with the var keyword.
Example
You can try to run the following code to learn how to create objects in JavaScript −
<html> <head> <title>User-defined objects</title> <script> var book = new Object(); // Create the object book.subject = "Perl"; // Assign properties to the object book.author = "Tutorialspoint"; </script> </head> <body> <script> document.write("Book name is : " + book.subject + "<br>"); document.write("Book author is : " + book.author + "<br>"); </script> </body> </html>
Output
Book name is : Perl Book author is : Tutorialspoint
- Related Questions & Answers
- How to create JavaScript objects using object() constructor?
- How to create JavaScript objects using new operator?
- How to create class objects in Python?
- Create many JavaScript objects as the same type?
- How to compare two objects in JavaScript?
- How to count JavaScript array objects?
- How to merge two JavaScript objects?
- How to create instance Objects using __init__ in Python?
- Why it is better not to create numbers as objects in JavaScript?
- How to create wrapper objects in JShell in Java 9?
- Different ways to create Objects in Java
- How to access nested json objects in JavaScript?
- How to shallow copy the objects in JavaScript?
- How to loop through arrays in JavaScript objects?
- How to create a third object from two objects using the key values in JavaScript?
Advertisements