From 7db72e5a01047cfac8a0fdf1e2d77304cfcb1106 Mon Sep 17 00:00:00 2001 From: Mustapha Anthonio Date: Mon, 27 Nov 2023 09:07:50 +0100 Subject: [PATCH 1/2] Added my name to the contributors list --- Contributors.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Contributors.md b/Contributors.md index a9e9ede..20240e9 100644 --- a/Contributors.md +++ b/Contributors.md @@ -5,4 +5,5 @@ 1. icodejsx 2. ogbon(Segun Amosu) 3. Solomon Eseme +4. Mustapha Anthonio From a62cd323da85a4bfed7e4e5d7ed4149bcf0fd408 Mon Sep 17 00:00:00 2001 From: Mustapha Anthonio Date: Mon, 27 Nov 2023 11:57:23 +0100 Subject: [PATCH 2/2] added symbol.md file --- symbol.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 symbol.md diff --git a/symbol.md b/symbol.md new file mode 100644 index 0000000..6516b0c --- /dev/null +++ b/symbol.md @@ -0,0 +1,50 @@ +In JavaScript, a symbol is a primitive data type introduced in ECMAScript 2015 (ES6). Symbols are unique and immutable values, often used as unique identifiers. Unlike strings, symbols are guaranteed to be unique, which makes them useful for scenarios where you want to create private or hidden object properties. + +Here's how you can create a symbol: + +```javascript +// Creating a symbol with an optional description +let mySymbol = Symbol("This is a symbol"); +``` + +Symbols are typically used as keys for object properties to avoid naming conflicts. Here's an example: + +```javascript +// Creating symbols +let symbol1 = Symbol("symbol1"); +let symbol2 = Symbol("symbol2"); + +// Creating an object with symbol keys +let myObject = { + [symbol1]: "Value for symbol1", + [symbol2]: "Value for symbol2" +}; + +// Accessing values using symbols +console.log(myObject[symbol1]); // Output: Value for symbol1 +console.log(myObject[symbol2]); // Output: Value for symbol2 +``` + +The `Symbol` function can be called with an optional description, which is useful for debugging and doesn't affect the uniqueness of the symbol. + +It's important to note that symbols are not enumerable in `for...in` loops and are not included in `Object.keys()` or `Object.getOwnPropertyNames()`. This makes them suitable for creating "private" properties in objects. + +Here's an example: + +```javascript +let myObject = { + publicProperty: "I'm public!", + [Symbol("privateProperty")]: "I'm private!" +}; + +console.log(Object.keys(myObject)); // Output: ["publicProperty"] +``` + +In this example, the symbol-keyed property is not visible when enumerating the object's keys. + +Symbols also have some predefined, well-known values called "symbolic properties." For example: + +- `Symbol.iterator`: A symbol that specifies an object's default iterator. +- `Symbol.match`, `Symbol.replace`, and `Symbol.search`: Symbols used for regular expression matching. + +Symbols provide a way to create truly unique and non-colliding identifiers in JavaScript. They are often used in conjunction with other features introduced in ES6 and later versions of the language. \ No newline at end of file