I bumbed into one of those moments when I just lose the focus and start wondering on a silly question:
var a = { b: "value" }
What is the typeof 'b' and I don't mean the typeof "value", but the actual Key labeled as b?
background: I started wondering about this when I had to create a key which is a string:
var a = { "b": "value" }
because at a later point it is referenced as:
a["b"]
And then eneded up wondering the original question.
keys() is a built-in JavaScript function that returns an array of the given object's property names in the same order as we get with a standard loop. The Object. keys() method takes an object as an argument and returns the array of strings representing all the enumerable properties of a given object.
An object contains properties, or key-value pairs. The desk object above has four properties. Each property has a name, which is also called a key, and a corresponding value. For instance, the key height has the value "4 feet" .
The array. keys() method is used to return a new array iterator which contains the keys for each index in the given input array. Parameters: This method does not accept any parameters. Return Values: It returns a new array iterator.
Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. As you can see in the image above, the key is the property, and each object value must have a key.
In object literal terms, b
is a property. Properties are either strings or symbols in JavaScript, although when defining the property name inside an object literal you may omit the string delimiters.
for (key in a) { alert(typeof key); //-> "string" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With