Why use . length no for?

Asked

Viewed 75 times

1

Eae , I’m studying by the book JS Definitive Guide and I’m seeing many examples where length is used in 'for', and I’m in doubt why use length if (from what I know) it only takes length if it’s a string. EX:

var x = 15
var y = 'haha'
console.log(x.length) // Undefined
console.log(y.length) // 4

Example of the Book

for(var i = 0; i < keys.length; i++) { // Para cada índice no array
var key = keys[i]; // Obtém a chave nesse índice
values[i] = o[key]; // Armazena o valor no array values
}

So why use length as in the above example ?

1 answer

3


In that case keys must be a Array.

For example:

const keys1 = ['key1', 'key2']
// keys1.length === 2

const keys2 = Object.keys({ key1: 10, key2: 20, key3: 10 });
// keys2.length === 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.