

Therefore a key is assigned to the constant array as well. The output is the printing key iterator and the key (index) number from 0 to 4. Later the keys() method is used which returns an iterator of this array.
#SCRIPT STUDIO KEY CODE#
As shown in the below example –Ĭonst colors = Įxplanation: As in the above code constant array is created of by using the const keyword, this keyword is used to creates a read-only reference to a variable or array.

In the next example code, we rewrite the above code for the javascript keys() method to apply on the constant array. Therefore a key is assigned to that hole but object.keys() method ignores the key(index) of holes in an array. And then the Object.keys() method is used which return keys of this array, as we can see in the output. If we want to gets only present elements key or to ignores the holes in an array we need to use Object.keys() method, as below:Įxplanation: As in the above code an array is created of which contain a hole or empty element. Therefore a key is assigned to that hole as well in the increasing order sequence and hence the keys() method does not ignore the holes in an array. This is an example for keys function in JavaScript Įxplanation: As in the above code an array is created of and the keys() method is used which return an iterator of this array, in this array, there are four elements and one hole are present. Next example we rewrite the above code where the javascript keys() method apply to an array where it contains missing elements or holes in between elements, as in the below code – Therefore a key is assigned to each element in an array according to the number of elements present. Var colors = Įxplanation: As in the above code an array is created of and the keys() method is used which return an iterator of this array, in this array, there are four elements present. This is an example for keys function in JavaScript īelow are the keys of an array :
