Open In App

JavaScript function* expression

Last Updated : 07 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The function* is an inbuilt keyword in JavaScript which is used to define a generator function inside an expression.

Syntax:

function* [name]([param1[, param2[, ..., paramN]]]) {
statements
}

Parameters: This function accepts the following parameter as mentioned above and described below:

  • name: This parameter is the function name.
  • paramN: This parameter is the name of an argument to be passed to the function.
  • statements: These parameters comprise the body of the function.

Example 1: Below examples illustrate the function* expression in JavaScript:

Javascript




// Illustration of function* expression
// use of function* keyword
function* func() {
    yield 1;
    yield 2;
    yield 3;
    yield " - Geeks";
}
 
let obj = '';
 
// Function calling
for (const i of func()) {
    obj = obj + i;
}
 
// Output
console.log(obj);


Output

123 - Geeks

Example 2: Below examples illustrate the function* expression in JavaScript:

Javascript




// Illustration of function* expression
// use of function* keyword
function* func2(y) {
    yield y * y;
};
 
function* func1() {
    for (let i = 1; i < 6; i++) {
        yield* func2(i);
    }
};
 
// Function calling
for (const x of func1()) {
 
    // Output
    console.log(x);
};


Output

1
4
9
16
25

Supported Browsers:

The browsers supported by JavaScript function* expression are listed below:

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 26 and above
  • Opera 36 and above
  • Safari 10 and above


Next Article

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 <script> 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
Difference between ‘function declaration’ and ‘function expression' in JavaScript
Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between ‘function declaration’ and ‘function expression’. The similarity is both use the keyword function and the most prominent difference is that the func
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
JavaScript Function Expression
The Javascript Function Expression is used to define a function inside any expression. The Function Expression allows us to create an anonymous function that doesn't have any function name which is the main difference between Function Expression and Function Declaration. A function expression can be used as an IIFE (Immediately Invoked Function Exp
2 min read
How to prevent overriding using Immediately Invoked Function Expression in JavaScript ?
Overriding is basically when you define multiple functions or variables that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed. Overriding usually happens when you have multiple javascript files in your page. It can be an external
2 min read
JavaScript async function expression
An async function expression is used to define an async function inside an expression in JavaScript. The async function is declared using the async keyword or the arrow syntax. Syntax: async function function_name (param1, param2, ..., paramN) { // Statements}Parameters: function_name: This parameter holds the function name. This function name is l
2 min read
Named Function Expression
In JavaScript or in any programming language, functions, loops, mathematical operators, and variables are the most widely used tools. This article is about how we can use and what are the real conditions when the Named function Expressions. We will discuss all the required concepts in this article to know the named function Expression in and out. N
3 min read
Expected an assignment or function call and instead saw an expression in ReactJS
In React.js we create components, inside these components, there are functions that we export and then use inside another component by importing it. Sometimes when you try to render that component or use that component as a tag inside another component, It throws an error "React: Expected an assignment or function call and instead saw an expression
3 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
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
How to check for IP address using regular expression in javascript?
The task is to validate the IP address of both IPv4 as well as IPv6. Here we are going to use RegExp to solve the problem. Approach 1: RegExp: Which split the IP address on. (dot) and check for each element whether they are valid or not(0-255). Example 1: This example uses the approach discussed above. C/C++ Code <h1 style="color:green;
1 min read
How to detect whether a device is iOS without using Regular Expression in JavaScript?
The task is to detect whether the device is iOS or not without using RegExp with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use navigator.platform property to check for the particular keywords which belongs to iOS devices using indexOf() method. Example: <!DOCTYPE html> <html> <head>
2 min read
Convert user input string into regular expression using JavaScript
In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object Regular expressions (RegExp) are patterns used to
2 min read
JavaScript yield* Expression
The yield* expression in JavaScript is used when one wants to delegate some other iterable object. This function iterates over the particular operand and yields each value that is returned by it. Syntax: yield* expression; Return Value: It returns the iterable object. Example 1: In this example, we will see the basic use of the Javascript yield* ex
2 min read
Javascript Program To Check For Balanced Brackets In An Expression (Well-Formedness) Using Stack
Given an expression string exp, write a program to examine whether the pairs and the orders of "{", "}", "(", ")", "[", "]" are correct in exp. Example:  Input: exp = "[()]{}{[()()]()}" Output: Balanced Input: exp = "[(])" Output: Not Balanced  Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.   Algorithm:  Declar
2 min read
How to build a Math Expression Tokenizer using JavaScript ?
A math expression tokenizer is a fundamental component in parsing mathematical expressions. It breaks down a mathematical expression into smaller units called tokens, which are easier to process and evaluate. In JavaScript, building a math expression tokenizer can be achieved through various approaches, each with its advantages and considerations.
2 min read
JavaScript program to Check the Expression has valid or Balanced Parenthesis or Not
Given the expression string, Our task is to Check whether the expression has valid or Balanced parenthesis or not in JavaScript. Valid input refers to every bracket having its corresponding bracket of the same type in the correct order. Example: Input: exp = "[()][()()]()" Output: True.Explanation: All of the brackets are balanced.Input: exp = "[(]
3 min read
How to Access Matched Groups in a JavaScript Regular Expression ?
Accessing matched groups in a JavaScript regular expression allows you to extract specific parts of a string based on patterns defined within parentheses in the regex pattern. This capability enables precise extraction and manipulation of text data, enhancing the versatility of regular expressions in string processing tasks. In this article, we wil
2 min read
How to Validate Email Address without using Regular Expression in JavaScript ?
Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server. An email address must have the following components to be considered valid:Username:
5 min read
JavaScript SyntaxError - Invalid regular expression flag "x"
This JavaScript exception invalid regular expression flag occurs if the flags, written after the second slash in RegExp literal, are not from either of (g, i, m, s, u, or y). Error Message on console: SyntaxError: Syntax error in regular expression (Edge) SyntaxError: invalid regular expression flag "x" (Firefox)SyntaxError: Invalid regular express
1 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. Example: Finding non-digit characters from given string [GFGTABS] JavaScript const regex = /[^0-9]/g; const str = "Hello123Geeks"; const result = str.match(r
2 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 (Regular Expression)
A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It's often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.). In JavaScript, RegExp is an object that is used to
4 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 |). [GFGTABS] JavaScript let str = "GEEKSFORGEEKS is the computer " + "science portal for geeks."; let regex = /(GEEKS|portal)/g; console.log(str.match(regex)); [/GFGTABS]Output[ 'GEEKS', 'GEEKS', 'portal' ] Syntax:/(x|y)/ /
1 min read
JavaScript - How to Validate Form Using Regular Expression?
To validate a form in JavaScript, you can use Regular Expressions (RegExp) to ensure that user input follows the correct format. In this article, we'll explore how to validate common form fields such as email, phone number, and password using RegExp patterns. 1. Validating an Email AddressOne of the most common form fields to validate is the email
4 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. [GFGTABS] JavaScript let str = "123abc790"; let regex = /[0-4]/g; console.log(str.match(regex)); [/GFGTABS]Output[ '1', '2', '3', '0' ] Syntax: /[0-9]/ // ornew R
1 min read
JavaScript - How to Create Regular Expression Only Accept Special Formula?
To ensure that a string matches a specific formula or pattern, you can use a regular expression (RegExp) in JavaScript. Here are the various ways to create a regular expression that only accepts regular formulas. 1: Alphanumeric Code FormulaLet's create a regular expression that matches a formula like an alphanumeric code of exactly 6 characters: 3
3 min read
JavaScript - How to Use a Variable in Regular Expression?
To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression. 1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using the RegExp constructor. This method allows you to
3 min read
How to validate HTML tag using Regular Expression
Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow one double quotes string, one single quotes string o
6 min read
  翻译: