What Does @@ Mean 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]

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.