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

symbols javascript

Symbols are primatives for unique values in javascript

> Symbol(value) returns a unique symbol

> Symbol.for(value) returns a unique symbol, but two calls using the 
same key will return the same symbol, within the scope

They can be useful because they are hidden from most iteration functions.
For ex, if you want to add a value to an object that you got back from a
third-party api. This will make sure your value doesn't appear in iteration
functions, and also won't require changing how the api is set up
Comment

javascript symbols

let sym1 = Symbol()
let sym2 = Symbol('foo')
let sym3 = Symbol('foo')
Comment

JavaScript Symbol

// two symbols with the same description

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

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

js symbole()

let sym = Symbol('foo')
typeof sym      // "symbol"
let symObj = Object(sym)
typeof symObj   // "object"
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

Symbols Javascript


let sym2 = Symbol('foo')
let sym3 = Symbol('foo')
 
		 console.log(Symbol(sym2==sym3)); /*false, symbols are guaranteed to be unique*/
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 :: switch statement 
Javascript :: middleware in express 
Javascript :: python json loads single quotes 
Javascript :: nodejs cluster 
Javascript :: how to get checked and unchecked checkbox value in jquery 
Javascript :: how to check if an element already exists in an array in javascript 
Javascript :: express router 
Javascript :: pdf to image javascript 
Javascript :: make button disabled if input is empty angular 
Javascript :: E.g query mongodb - node 
Javascript :: add new element to existing json object 
Javascript :: lettre au hasard javascript 
Javascript :: set timer 
Javascript :: object destruction in javascript 
Javascript :: ex:js 
Javascript :: what is javascript 
Javascript :: router 
Javascript :: js alerts 
Javascript :: how to check if a number is negative in p5.js 
Javascript :: list of javascript cheat sheet 
Javascript :: node.js server-side javascript 
Javascript :: get table schema with knex 
Javascript :: where to add const form = document.querySelector(".top-banner form"); form.addEventListener("submit", e = { e.preventDefault(); const inputVal = input.value; }); 
Javascript :: immutablejs update use 
Javascript :: chartjs angular bar beginAtZero 
Javascript :: tomtom map in vuejs 
Javascript :: warn user before leaving page angular 
Javascript :: open bytes in new tab angular 
Javascript :: pebbel if statement check boolean 
Javascript :: paypal cordova can not open popup window - blocked 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =