Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of "keys" in JavaScript?

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.

like image 638
tunylund Avatar asked Aug 31 '10 10:08

tunylund


People also ask

What are keys in JavaScript?

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.

What are keys and values in JavaScript?

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" .

What is the keys of an array in JavaScript?

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.

Does JavaScript object have key?

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.


1 Answers

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" } 
like image 184
Andy E Avatar answered Oct 05 '22 15:10

Andy E