Is there a way to find all the attributes of a primitive in JavaScript?

58 Views Asked by At

In Python, for example, I can use

dir(str)

to find all the attributes and methods of the string data type.

Is there something similar in Javascript?

Just want additional info on JavaScript primitives. It's not an actual coding problem. Thanks.

1

There are 1 best solutions below

2
Dimava On BEST ANSWER
let o = Object.getPrototypeOf('string') // equals to String.prototype

for (let k of Object.getOwnPropertyNames(o)) {
    console.log(k, o[k])
}

same for all other primitives (number 123, BigInt 123n, symbol Symbol('symbolDesc'))

Note however that some of them may be deprecated (mainnly string functions which wrap text.bold() == '<b>'+text+'</b>' and alike)