Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

what is symbol in javascript

Symbol is a primitive data type of JS along with string,number,bool,null and undef
which are used to identify object properties since none is equal to the other.
Comment

Symbol Properties javascript

const x = Symbol('hey');

// description property
console.log(x.description); // hey

const stringArray = ['a', 'b', 'c'];
const numberArray = [1, 2, 3];

// isConcatSpreadable property
numberArray[Symbol.isConcatSpreadable] = false;

let result = stringArray.concat(numberArray);
console.log(result); // ["a", "b", "c", [1, 2, 3]]
Comment

Symbol Methods javascript

// get symbol by name
let sym = Symbol.for('hello');
let sym1 = Symbol.for('id');

// get name by symbol
console.log( Symbol.keyFor(sym) ); // hello
console.log( Symbol.keyFor(sym1) ); // id
Comment

javascript Symbol Methods

for()	Searches for existing symbols
keyFor()	Returns a shared symbol key from the global symbol registry.
toSource()	Returns a string containing the source of the Symbol object
toString()	Returns a string containing the description of the Symbol
valueOf()	Returns the primitive value of the Symbol object.
Comment

JavaScript Symbol

// two symbols with the same description

const value1 = Symbol('hello');
const value2 = Symbol('hello');

console.log(value1 === value2); // false
Comment

Symbol Methods javascript

// get symbol by name
let sym = Symbol.for('hello');
let sym1 = Symbol.for('id');

// get name by symbol
console.log( Symbol.keyFor(sym) ); // hello
console.log( Symbol.keyFor(sym1) ); // id
Comment

JavaScript Symbol

// two symbols with the same description

const value1 = Symbol('hello');
const value2 = Symbol('hello');
Comment

javascript Symbol Properties

asyncIterator	Returns the default AsyncIterator for an object
hasInstance	Determines if a constructor object recognizes an object as its instance
isConcatSpreadable	Indicates if an object should be flattened to its array elements
iterator	Returns the default iterator for an object
match	Matches against a string
matchAll	Returns an iterator that yields matches of the regular expression against a string
replace	Replaces matched substrings of a string
search	Returns the index within a string that matches the regular expression
split	Splits a string at the indices that match a regular expression
species	Creates derived objects
toPrimitive	Converts an object to a primitive value
toStringTag	Gives the default description of an object
description	Returns a string containing the description of the symbol
Comment

what does the symbol function do in javascript

var idk={test:true,[Symbol.toStringTag]:'e'}
consoel.log(idk)//e {test:true,[Symbol.toStringTag]:'e'}
idk.toString()//[object e]
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript modules 
Javascript :: javascript Arrow Function as an Expressio 
Javascript :: javascript Tagged Templates 
Javascript :: javascript WeakMaps Are Not iterable 
Javascript :: creating js classes 
Javascript :: JavaScript pauses the async function until the promise 
Javascript :: javascript Octal syntax is not allowed 
Javascript :: JavaScript Code Blocks 
Javascript :: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. 
Javascript :: alphanumeric without space regex 
Javascript :: JavaScript / jQuery HTML Elements 
Javascript :: javascript get days difference between two dates 
Javascript :: jQuery Prevent Submit on Enter Key Except in Textarea 
Javascript :: roman to integer fastest way 
Javascript :: reactjs libphonenumber 
Javascript :: phaser set alpha 
Javascript :: phaser play animation after repeat 
Javascript :: apply multiple style objects in react js 
Javascript :: when end sound show alert 
Javascript :: JavaScript date format 2 
Javascript :: how to get params from function js 
Javascript :: string concat in js 
Javascript :: how to declare a variable js 
Javascript :: loop on each character js 
Javascript :: extract data from pdf nodejs 
Javascript :: asp net core use newtonsoft json 
Javascript :: object length 
Javascript :: pass object by value js 
Javascript :: javascript array last element get 
Javascript :: delegate in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =