
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is a class in JavaScript?
class
A class is a type of function, but instead of using the keyword 'function', keyword 'class' is used to initiate it, and the properties are assigned inside a constructor() method. The constructor() method is called each time the class object is initialized.
Example-1
In the following example, a class called 'company' is created and inside a constructor() method the name of the company is assigned and displayed the result in the output.
<html> <body> <p id="class"></p> <script> class Company { constructor(branch) { this.name = branch; } } myComp = new Company("Tutorialspoint"); document.getElementById("class").innerHTML = myComp.name; </script> </body> </html>
Output
Tutorialspoint
Example-2
<html> <body> <script> class Company { constructor(branch) { this.name = branch; } } myComp = new Company("Rk enterprises"); document.write(myComp.name); </script> </body> </html>
Output
Rk enterprises
- Related Articles
- What is the “get” keyword before a function in a class - JavaScript?
- What is the class "class" in Java?
- What is a static class in Java?
- What is a singleton class in Java?
- What is a Throwable class in Java?
- What is a Locale class in Java?
- What is a sealed class in C#?
- What is a static class in C#?
- What is a base class in C#?
- What is a Nested Class in Java?
- What is a Quartet class in JavaTuples?
- What is a Unit class in JavaTuples?
- Why is 'class' a reserved word in JavaScript?
- What is a virtual base class in C++?
- What is a non-static class in C#?

Advertisements