Here’s an approach that might be easier to understand.
This code logs an error if one or more properties are not a string:
var obj = {
"val1": "test1",
"val2": 1,
"val3": null
};
for (var property in obj) {
if (typeof obj[property] !== 'string') {
console.error(property + ' is not a string!');
}
}
PS: You had some errors in your code:
- Don’t add a comma after the last property, it may lead to errors
- You have overwritten the same property over and over again
- You where missing a semicolon after the closing curly bracket of your object, may also lead to errors
2
solved check if a value is string in an object javascript