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

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

Problem Description:

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.

Solution – 1

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)

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject