What Does @@ Mean in JavaScript?

Learn what the double at-sign (@@) means in JavaScript

The double at sign (@@) is used for referring to a "well-known symbol" — which are built-in symbols. However, the double at sign itself is not a valid JavaScript syntax.

Following is a list of some well-known symbols in JavaScript:

Name Refers to
@@hasInstance Symbol.hasInstance
@@isConcatSpreadable Symbol.isConcatSpreadable
@@iterator Symbol.iterator
@@match Symbol.match
@@replace Symbol.replace
@@search Symbol.search
@@species Symbol.species
@@split Symbol.split
@@toPrimitive Symbol.toPrimitive
@@toStringTag Symbol.toStringTag
@@unscopables Symbol.unscopables

For example, you can implement [@@iterator] (or Symbol.iterator) as a generator method, to make a class object iterable:

// ES6+
class Foo {
    *[Symbol.iterator]() {
        yield 1;
        yield 2;
        yield 3;
    }
}

const foo = new Foo();

for (const val of foo) {
    console.log(val); // 1 2 3
}

console.log([ ...foo ]); // [1, 2, 3]

The same can be implemented in an object literal, for example, like so:

// ES6+
const obj = {
    [Symbol.iterator]: function* () {
        yield 1;
        yield 2;
        yield 3;
    },
};

for (const val of obj) {
    console.log(val); // 1 2 3
}

console.log([ ...obj ]); // [1, 2, 3]

Hope you found this post useful. It was published . Please show your love and support by sharing this post.