recursal/MDN huggingface dataset:
Bitwise AND assignment (&=)
===========================

The **bitwise AND assignment (`&=`)** operator performs bitwise AND on the two operands and assigns the result to the left operand.

Try it
------

Syntax
------


```js
x &= y
```

Description
-----------

`x &= y` is equivalent to `x = x & y`, except that the expression `x` is only evaluated once.

Examples
--------

### Using bitwise AND assignment


```js
let a = 5;
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
a &= 2; // 0

let b = 5n;
b &= 2n; // 0n
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-assignment-operators |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Assignment operators in the JS guide
* Bitwise AND (`&`)
Less than or equal (<=)
=======================

The **less than or equal (`<=`)** operator returns `true` if the left operand is less than or equal to the right operand, and `false` otherwise.

Try it
------

Syntax
------


```js
x <= y
```

Description
-----------

The operands are compared using the same algorithm as the Less than operator, with the operands swapped and the result negated. `x <= y` is generally equivalent to `!(y < x)`, except for two cases where `x <= y` and `x > y` are both `false`:

* If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws a syntax error when passed to `BigInt()`).
* If one of the operands gets converted to `NaN`. (For example, strings that cannot be converted to numbers, or `undefined`.)

In addition, `x <= y` coerces `x` to a primitive before `y`, while `y < x` coerces `y` to a primitive before `x`. Because coercion may have side effects, the order of the operands may matter.

`x <= y` is generally equivalent to `x < y || x == y`, except for a few cases:

* When one of `x` or `y` is `null`, and the other is something that's not `null` and becomes 0 when coerced to numeric (including `0`, `0n`, `false`, `""`, `"0"`, `new Date(0)`, etc.): `x <= y` is `true`, while `x < y || x == y` is `false`.
* When one of `x` or `y` is `undefined`, and the other is one of `null` or `undefined`: `x <= y` is `false`, while `x == y` is `true`.
* When `x` and `y` are the same object that becomes `NaN` after the first step of Less than (such as `new Date(NaN)`): `x <= y` is `false`, while `x == y` is `true`.
* When `x` and `y` are different objects that become the same value after the first step of Less than: `x <= y` is `true`, while `x < y || x == y` is `false`.
Examples
--------

### String to string comparison


```js
"a" <= "b"; // true
"a" <= "a"; // true
"a" <= "3"; // false
```

### String to number comparison


```js
"5" <= 3; // false
"3" <= 3; // true
"3" <= 5; // true

"hello" <= 5; // false
5 <= "hello"; // false
```

### Number to Number comparison


```js
5 <= 3; // false
3 <= 3; // true
3 <= 5; // true
```

### Number to BigInt comparison


```js
5n <= 3; // false
3 <= 3n; // true
3 <= 5n; // true
```

### Comparing Boolean, null, undefined, NaN


```js
true <= false; // false
true <= true; // true
false <= true; // true

true <= 0; // false
true <= 1; // true

null <= 0; // true
1 <= null; // false

undefined <= 3; // false
3 <= undefined; // false

3 <= NaN; // false
NaN <= 3; // false
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-relational-operators |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Greater than (`>`)
* Greater than or equal (`>=`)
* Less than (`<`)
Word boundary assertion: \b, \B
===============================

A **word boundary assertion** checks if the current position in the string is a word boundary. A word boundary is where the next character is a word character and the previous character is not a word character, or vice versa.

Syntax
------


```regex
\b
\B
```

Description
-----------

`\b` asserts that the current position in the string is a word boundary. `\B` negates the assertion: it asserts that the current position is not a word boundary. Both are *assertions*, so unlike other character escapes or character class escapes, `\b` and `\B` don't consume any characters.

A word character includes the following:

* Letters (A–Z, a–z), numbers (0–9), and underscore (\_).
* If the regex is Unicode-aware and the `i` flag is set, other Unicode characters that get canonicalized to one of the characters above through case folding.

Word characters are also matched by the `\w` character class escape.

Out-of-bounds input positions are considered non-word characters. For example, the following are successful matches:


```js
/\ba/.exec("abc");
/c\b/.exec("abc");

/\B /.exec(" abc");
/ \B/.exec("abc ");
```

Examples
--------

### Detecting words

The following example detects if a string contains the word "thanks" or "thank you":


```js
function hasThanks(str) {
  return /\b(thanks|thank you)\b/i.test(str);
}

hasThanks("Thanks! You helped me a lot."); // true
hasThanks("Just want to say thank you for all your work."); // true
hasThanks("Thanksgiving is around the corner."); // false
```


**Warning:** Not all languages have clearly defined word boundaries. If you are working with languages like Chinese or Thai, where there are no whitespace separators, use a more advanced library like `Intl.Segmenter` to search for words instead.

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # prod-Assertion |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Assertions guide
* Regular expressions
* Input boundary assertion: `^`, `$`
* Lookahead assertion: `(?=...)`, `(?!...)`
* Lookbehind assertion: `(?<=...)`, `(?<!...)`
* Character escape: `\n`, `\u{...}`
Wildcard: .
===========

A **wildcard** matches all characters except line terminators. It also matches line terminators if the `s` flag is set.

Syntax
------


```regex
.
```

Description
-----------

`.` matches any character except line terminators. If the `s` flag is set, `.` also matches line terminators.

The exact character set matched by `.` depends on whether the regex is Unicode-aware. If it is Unicode-aware, `.` matches any Unicode code point; otherwise, it matches any UTF-16 code unit. For example:


```js
/../.test("😄"); // true; matches two UTF-16 code units as a surrogate pair
/../u.test("😄"); // false; input only has one Unicode character
```

Examples
--------

### Usage with quantifiers

Wildcards are often used with quantifiers to match any character sequence, until the next character of interest is found. For example, the following example extracts the title of a Markdown page in the form `# Title`:


```js
function parseTitle(entry) {
  // Use multiline mode because the title may not be at the start of
  // the file. Note that the m flag does not make . match line
  // terminators, so the title must be on a single line
  // Return text matched by the first capturing group.
  return /^#[ \t]+(.+)$/m.exec(entry)?.[1];
}

parseTitle("# Hello world"); // "Hello world"
parseTitle("## Subsection"); // undefined
parseTitle(`
---
slug: Web/JavaScript/Reference/Regular\_expressions/Wildcard
---

# Wildcard: .

A \*\*wildcard\*\* matches all characters except line terminators.
`); // "Wildcard: ."
```

### Matching code block content

The following example matches the content of a code block enclosed by three backticks in Markdown. It uses the `s` flag to make `.` match line terminators, because the content of a code block may span multiple lines:


```js
function parseCodeBlock(entry) {
  return /^```.\*?^(.+?)\n```/ms.exec(entry)?.[1];
}

parseCodeBlock(`
\`\`\`js
console.log("Hello world");
\`\`\`
`); // "console.log("Hello world");"

parseCodeBlock(`
A \`try...catch\` statement must have the blocks enclosed in curly braces.

\`\`\`js example-bad
try
 doSomething();
catch (e)
 console.log(e);
\`\`\`
`); // "try\n doSomething();\ncatch (e)\n console.log(e);"
```


**Warning:** These examples are for demonstration only. If you want to parse Markdown, use a dedicated Markdown parser because there are many edge cases to consider.

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # prod-Atom |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Character classes guide
* Regular expressions
* Character class: `[...]`, `[^...]`
* Character class escape: `\d`, `\D`, `\w`, `\W`, `\s`, `\S`
Named backreference: \k<name>
=============================

A **named backreference** refers to the submatch of a previous named capturing group and matches the same text as that group. For unnamed capturing groups, you need to use the normal backreference syntax.

Syntax
------


```regex
\k<name>
```

### Parameters

`name`

The name of the group. Must be a valid identifier and refer to an existent named capturing group.

Description
-----------

Named backreferences are very similar to normal backreferences: it refers to the text matched by a capturing group and matches the same text. The difference is that you refer to the capturing group by name instead of by number. This makes the regular expression more readable and easier to refactor and maintain.

In Unicode-unaware mode, the sequence `\k` only starts a named backreference if the regex contains at least one named capturing group. Otherwise, it is an identity escape and is the same as the literal character `k`. This is a deprecated syntax for web compatibility, and you should not rely on it.


```js
/\k/.test("k"); // true
```

Examples
--------

### Pairing quotes

The following function matches the `title='xxx'` and `title="xxx"` patterns in a string. To ensure the quotes match, we use a backreference to refer to the first quote. Accessing the second capturing group (`[2]`) returns the string between the matching quote characters:


```js
function parseTitle(metastring) {
  return metastring.match(/title=(?<quote>["'])(.\*?)\k<quote>/)[2];
}

parseTitle('title="foo"'); // 'foo'
parseTitle("title='foo' lang='en'"); // 'foo'
parseTitle('title="Named capturing groups\' advantages"'); // "Named capturing groups' advantages"
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # prod-AtomEscape |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Groups and backreferences guide
* Regular expressions
* Capturing group: `(...)`
* Named capturing group: `(?<name>...)`
* Backreference: `\1`, `\2`
Character class escape: \d, \D, \w, \W, \s, \S
==============================================

A **character class escape** is an escape sequence that represents a set of characters.

Syntax
------


```regex
\d, \D
\s, \S
\w, \W
```


**Note:** `,` is not part of the syntax.

Description
-----------

Unlike character escapes, character class escapes represent a predefined *set* of characters, much like a character class. The following character classes are supported:

`\d`

Matches any digit character. Equivalent to `[0-9]`.

`\w`

Matches any word character, where a word character includes letters (A–Z, a–z), numbers (0–9), and underscore (\_). If the regex is Unicode-aware and the `i` flag is set, it also matches other Unicode characters that get canonicalized to one of the characters above through case folding.

`\s`

Matches any whitespace or line terminator character.

The uppercase forms `\D`, `\W`, and `\S` create complement character classes for `\d`, `\w`, and `\s`, respectively. They match any character that is not in the set of characters matched by the lowercase form.

Unicode character class escapes start with `\p` and `\P`, but they are only supported in Unicode-aware mode. In Unicode-unaware mode, they are identity escapes for the `p` or `P` character.

Character class escapes can be used in character classes. However, they cannot be used as boundaries of character ranges, which is only allowed as a deprecated syntax for web compatibility, and you should not rely on it.

Examples
--------

### Splitting by whitespace

The following example splits a string into an array of words, supporting all kinds of whitespace separators:


```js
function splitWords(str) {
  return str.split(/\s+/);
}

splitWords(`Look at the stars
Look how they\tshine for you`);
// ['Look', 'at', 'the', 'stars', 'Look', 'how', 'they', 'shine', 'for', 'you']
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # prod-CharacterClassEscape |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Character classes guide
* Regular expressions
* Character class: `[...]`, `[^...]`
* Unicode character class escape: `\p{...}`, `\P{...}`
* Character escape: `\n`, `\u{...}`
* Disjunction: `|`
Disjunction: |
==============

A **disjunction** specifies multiple alternatives. Any alternative matching the input causes the entire disjunction to be matched.

Syntax
------


```regex
alternative1|alternative2
alternative1|alternative2|alternative3|…
```

### Parameters

`alternativeN`

One alternative pattern, composed of a sequence of atoms and assertions. Successfully matching one alternative causes the entire disjunction to be matched.

Description
-----------

The `|` regular expression operator separates two or more *alternatives*. The pattern first tries to match the first alternative; if it fails, it tries to match the second one, and so on. For example, the following matches `"a"` instead of `"ab"`, because the first alternative already matches successfully:


```js
/a|ab/.exec("abc"); // ['a']
```


The `|` operator has the lowest precedence in a regular expression. If you want to use a disjunction as a part of a bigger pattern, you must group it.

When a grouped disjunction has more expressions after it, the matching begins by selecting the first alternative and attempting to match the rest of the regular expression. If the rest of the regular expression fails to match, the matcher tries the next alternative instead. For example,


```js
/(?:(a)|(ab))(?:(c)|(bc))/.exec("abc"); // ['abc', 'a', undefined, undefined, 'bc']
// Not ['abc', undefined, 'ab', 'c', undefined]
```


This is because by selecting `a` in the first alternative, it's possible to select `bc` in the second alternative and result in a successful match. This process is called *backtracking*, because the matcher first goes beyond the disjunction and then comes back to it when subsequent matching fails.

Note also that any capturing parentheses inside an alternative that's not matched produce `undefined` in the resulting array.

An alternative can be empty, in which case it matches the empty string (in other words, always matches).

Alternatives are always attempted left-to-right, regardless of the direction of matching (which is reversed in a lookbehind).

Examples
--------

### Matching file extensions

The following example matches file extensions, using the same code as the input boundary assertion article:


```js
function isImage(filename) {
  return /\.(?:png|jpe?g|webp|avif|gif)$/i.test(filename);
}

isImage("image.png"); // true
isImage("image.jpg"); // true
isImage("image.pdf"); // false
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # prod-Disjunction |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Character classes guide
* Regular expressions
* Quantifier: `*`, `+`, `?`, `{n}`, `{n,}`, `{n,m}`
* Character class: `[...]`, `[^...]`
arguments.length
================

The **`arguments.length`** data property contains the number of arguments passed to the function.

Value
-----

A non-negative integer.

| Property attributes of `arguments.length` |
| --- |
| Writable | yes |
| Enumerable | no |
| Configurable | yes |

Description
-----------

The `arguments.length` property provides the number of arguments actually passed to a function. This can be more or less than the defined parameter's count (see `Function.prototype.length`). For example, for the function below:


```js
function func1(a, b, c) {
  console.log(arguments.length);
}
```


`func1.length` returns `3`, because `func1` declares three formal parameters. However, `func1(1, 2, 3, 4, 5)` logs `5`, because `func1` was called with five arguments. Similarly, `func1(1)` logs `1`, because `func1` was called with one argument.

Examples
--------

### Using arguments.length

In this example, we define a function that can add two or more numbers together.


```js
function adder(base /\*, num1, …, numN \*/) {
  base = Number(base);
  for (let i = 1; i < arguments.length; i++) {
    base += Number(arguments[i]);
  }
  return base;
}
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-arguments-exotic-objects |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Functions guide
* Functions
* `arguments`
* `Function`: `length`
arguments[@@iterator]()
=======================

The **`[@@iterator]()`** method of `arguments` objects implements the iterable protocol and allows `arguments` objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and `for...of` loops. It returns an array iterator object that yields the value of each index in the `arguments` object.

The initial value of this property is the same function object as the initial value of the `Array.prototype.values` property (and also the same as `Array.prototype[@@iterator]`).

Syntax
------


```js
arguments[Symbol.iterator]()
```

### Parameters

None.

### Return value

The same return value as `Array.prototype.values()`: a new iterable iterator object that yields the value of each index in the `arguments` object.

Examples
--------

### Iteration using for...of loop

Note that you seldom need to call this method directly. The existence of the `@@iterator` method makes `arguments` objects iterable, and iterating syntaxes like the `for...of` loop automatically call this method to obtain the iterator to loop over.


```js
function f() {
  for (const letter of arguments) {
    console.log(letter);
  }
}
f("w", "y", "k", "o", "p");
```

### Manually hand-rolling the iterator

You may still manually call the `next()` method of the returned iterator object to achieve maximum control over the iteration process.


```js
function f() {
  const argsIter = arguments[Symbol.iterator]();
  console.log(argsIter.next().value); // a
  console.log(argsIter.next().value); // b
  console.log(argsIter.next().value); // c
  console.log(argsIter.next().value); // d
  console.log(argsIter.next().value); // e
}
f("w", "y", "k", "o", "p");
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-createunmappedargumentsobject |
| ECMAScript Language Specification # sec-createmappedargumentsobject |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Functions guide
* Functions
* `arguments`
* `Array.prototype.values()`
* `Symbol.iterator`
* Iteration protocols
return
======

The **`return`** statement ends function execution and specifies a value to be returned to the function caller.

Try it
------

Syntax
------


```js
return;
return expression;
```


`expression` Optional

The expression whose value is to be returned. If omitted, `undefined` is returned.

Description
-----------

The `return` statement can only be used within function bodies. When a `return` statement is used in a function body, the execution of the function is stopped. The `return` statement has different effects when placed in different functions:

* In a plain function, the call to that function evaluates to the return value.
* In an async function, the produced promise is resolved with the returned value.
* In a generator function, the produced generator object's `next()` method returns `{ done: true, value: returnedValue }`.
* In an async generator function, the produced async generator object's `next()` method returns a promise fulfilled with `{ done: true, value: returnedValue }`.

If a `return` statement is executed within a `try` block, its `finally` block, if present, is first executed, before the value is actually returned.

### Automatic semicolon insertion

The syntax forbids line terminators between the `return` keyword and the expression to be returned.


```js
return
a + b;
```


The code above is transformed by automatic semicolon insertion (ASI) into:


```js
return;
a + b;
```


This makes the function return `undefined` and the `a + b` expression is never evaluated. This may generate a warning in the console.

To avoid this problem (to prevent ASI), you could use parentheses:


```js
return (
  a + b
);
```

Examples
--------

### Interrupt a function

A function immediately stops at the point where `return` is called.


```js
function counter() {
  // Infinite loop
  for (let count = 1; ; count++) {
    console.log(`${count}A`); // Until 5
    if (count === 5) {
      return;
    }
    console.log(`${count}B`); // Until 4
  }
  console.log(`${count}C`); // Never appears
}

counter();

// Logs:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A
```

### Returning a function

See also the article about Closures.


```js
function magic() {
  return function calc(x) {
    return x \* 42;
  };
}

const answer = magic();
answer(1337); // 56154
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-return-statement |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Functions
* Closures