Open In App

JavaScript class expression

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript class is a type of function declared with class keyword, that is used to implement object-oriented paradigm. Constructors are used to initialize the attributes of a class. There are 2 ways to creating class in JavaScript.

  • class declaration
  • class expression

In this article, we’ll discuss class expression to declare classes in JavaScript and how to use them.

class expression:  The class expression is another way of creating classes in JavaScript and they can be named or unnamed. If named, the class name is used internally, but not outside of the class.

Syntax:

  • Using named class expression:
const variable_name = new Class_name {
    // class body
}
  • Using unnamed class expression:
const variable_name = class{
     //class body
}

Example 1: Named class expression:

Javascript




<script>
const Website = class Geek {
  constructor(name){
      this.name = name;
  }
  websiteName() {
    return this.name;
  }
};
  
const x = new Website("GeeksforGeeks");
console.log(x.websiteName());
</script>


Output:

GeeksforGeeks

Example 2: Unnamed class expression:

Javascript




<script>
const Website = class {
  constructor(name) {
    this.name = name;
  }
  returnName() {
    return this.name;
  }
};
  
console.log(new Website("GeeksforGeeks").returnName());
</script>


 
 

Output:

 

GeeksforGeeks

 

Supported Browser:

  • Chrome 42 and above
  • Edge 13 and above
  • Firefox 45 and above
  • Opera 29 and above
  • Safari 7 and above


Similar Reads

Difference between AngularJS Expression and Angular Expression
AngularJS is a JavaScript-based framework that can be used by adding it to an HTML page using a &lt;script&gt; tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular on the other hand is a client-side TypeScript-based, front-end web framework by Google. Angular is
3 min read
AngularJS ng-class if-else Expression
In the AngularJS framework, Conditional statements, like if-else expressions, are a feature that helps many web developers like us to apply different conditions in AngularJS applications. This can be used with the ng-class directive that allows us to control the styling of the elements based on the change of variables and other user inputs. These i
5 min read
How to clone a given regular expression in JavaScript ?
In this article, we will know How to clone a regular expression using JavaScript. We can clone a given regular expression using the constructor RegExp(). The syntax of using this constructor has been defined as follows:- Syntax: new RegExp(regExp , flags) Here regExp is the expression to be cloned and flags determine the flags of the clone. There a
2 min read
JavaScript RegExp (x|y) Expression
The RegExp (x|y) Expression in JavaScript is used to search any of the specified characters (separated by |). Syntax: /(x|y)/ or new RegExp("(x|y)") Syntax with modifiers: /(x|y)/g or new RegExp("(x|y)", "g") Example 1: This example searches the word "GEEKS" or "portal" in the whole string. C/C++ Code function geek() { let str1 = &quot;GEEKSFORGEEK
2 min read
How to return all matching strings against a regular expression in JavaScript ?
In this article, we will learn how to identify if a string matches with a regular expression and subsequently return all the matching strings in JavaScript. We can use the JavaScript string.search() method to search for a match between a regular expression in a given string. Syntax: let index = string.search( expression )Parameters: This method acc
3 min read
JavaScript RegExp [abc] Expression
The RegExp [abc] Expression in JavaScript is used to search any character between the brackets. The character inside the brackets can be a single character or a span of characters. [A-Z]: It is used to match any character from uppercase A to Z.[a-z]: It is used to match any character from lowercase a to z.[A-z]: It is used to match any character fr
2 min read
JavaScript RegExp [^abc] Expression
The RegExp [^abc] Expression in JavaScript is used to search for any character which is not between the brackets. The character inside the brackets can be a single character or a span of characters. [A-Z]: It is used to match any character from uppercase A to uppercase Z.[a-z]: It is used to match any character from lowercase a to lowercase z.[A-z]
2 min read
JavaScript RegExp [0-9] Expression
The RegExp [0-9] Expression in JavaScript is used to search any digit which is between the brackets. The character inside the brackets can be a single digit or a span of digits. Syntax: /[0-9]/ or new RegExp("[0-9]") Syntax with modifiers: /[0-9]/g or new RegExp("[0-9]", "g") Example 1: This example searches the digits between [0-4] in the whole st
2 min read
JavaScript RegExp [^0-9] Expression
The RegExp [^0-9] Expression in JavaScript is used to search any digit which is not between the brackets. The character inside the brackets can be a single digit or a span of digits. Syntax: /[^0-9]/ or new RegExp("[^0-9]") Syntax with modifiers: /[^0-9]/g or new RegExp("[^0-9]", "g") Example 1: This example searches the digits which are not presen
2 min read
Difference between function expression vs declaration in JavaScript
Function Declaration: A Function Declaration( or a Function Statement) defines a function with the specified parameters without requiring a variable assignment. They exist on their own, i.e, they are standalone constructs and cannot be nested within a non-function block. A function is declared using the function keyword. Syntax:function gfg(paramet
1 min read