I want the following code to be executed ,what exactly should i put the value of token,
Does putting token ={} makes the length as 0 or token ='' ,can anyone explain?
if (Object.keys(token).length == 0) {//api call to geneate token}
I want the following code to be executed ,what exactly should i put the value of token,
Does putting token ={} makes the length as 0 or token ='' ,can anyone explain?
if (Object.keys(token).length == 0) {//api call to geneate token}
Since Object.keys is expecting an object, token={} is the right choice. This is called an "Object literal".
The "length" property is only for array, string. OR, you can always define a "length" property for your objects (classes). In your case, you want to check "token" got any properties. The "Object.keys(token)" gives you an array of token properties. Then you use ".length" to check if it " === 0", means no properties.
let token = {};
Object.keys(token).length;
This will return length as 0. As Object.keys() will return the array of keys of an object and .lenght will return the count of the array.