Introduction
- Creating Class in JavaScript the following syntax is used for declaring a class in JavaScript:Here Emp can act as a class in JavaScript.
- function Emp() {
- alert('Emp instantiated');
- }
The body of Emp acts as a constructor and is called as soon as we create an object of the class.
- Creating Objects of Emp Class
Using the following syntax we can create an object of the Emp class:As soon as we create an object of the Emp class the constructor will be called.
- var Emp1 = new Emp();
Here the above function Emp would be treated as a class in JavaScript.
- Running the code
An alert will be shown on the page-load of the web form.
- Properties of class
We can define the properties of a class in the following way:
- function Emp(firstname) {
- alert('Emp instantiated');
- this.firstname = firstname;
- }
- Passing value to properties
- var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");
- alert(Emp1.firstname);
- Snap for defining properties

- Complete code

- Running code
When we run the code we get an alert of the string that we are passing to the Emp class.- var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");
Conclusion
Next article: Object-Oriented Programming in JavaScript -Part 2

Join the conversation! Your thoughts help the community grow.