A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the
object, but also keys contained in the prototype of that object. It is generally
a best practice to check for these keys specifically:
varsomeObject;for(varkeyinsomeObject){if(!someObject.hasOwnProperty(key)){continue;// Skip keys from the prototype.}doSomethingWith(key);}
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally.
This is optional if there is only one statement, but leaving them out can lead to unexpected
behaviour if another statement is added later.
Consider:
if(a>0)b=42;
If you or someone else later decides to put another statement in, only the first
statement will be executed.
if(a>0)console.log("a > 0");b=42;
In this case the statement b=42 will always be executed, while
the logging statement will be executed conditionally.
if(a>0){console.log("a > 0");b=42;}
ensures that the proper code will be executed conditionally no matter how
many statements are added or removed.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: