Metaprogramming in JavaScript

Introduction to JavaScript “Symbols” and their use in Metaprogramming

In this lesson, we are going to learn about JavaScript symbols and some of the new JavaScript features that depend on them. We will also discuss how they aid metaprogramming.

Uday Hiwarale
JsPoint
Published in
16 min readAug 14, 2020

--

(source: unsplash.com)

What are the primitive data types in JavaScript? Well, they are null, undefined, string, number and boolean. Do we need more? Yes. The symbol is a new primitive data type introduced in ES2015 (ES6) along with bigint which was also introduced in this revision.

typeof null;      // 'object' (it's a bug)
typeof undefined; // 'undefined'
typeof 100; // 'number'
typeof "hello"; // 'string'
typeof true; // 'boolean'
typeof Symbol(); // 'symbol'
typeof 100n; // 'bigint'

In this lesson, we are just going to talk about symbols and leave others for another day. Symbols are fascinating and not like any other data type you have seen before. For the starters, the symbol is a primitive data type, however, you can’t write it in literal form because it doesn’t have one.

var sym = Symbol(description);

--

--