What is difference between them in JavaScript:
- var obj1 = { age:22}
- var obj2 = { "age":22}
In JavaScript, there is no difference between these two object definitions:
- var obj1 = { age: 22 };
- var obj2 = { "age": 22 };
Both obj1 and obj2 define an object with a single property age set to 22.
In JavaScript, age (without quotes) and "age" (with quotes) are both valid syntax for defining object keys.
Property names are automatically interpreted as strings, so { age: 22 } and { "age": 22 } are equivalent.
This flexibility allows you to omit quotes for keys that follow identifier naming rules (such as not starting with a number or containing spaces). However, if the property name contains spaces or special characters, you would need to use quotes. For example:
var obj3 = { "full name": "John Doe" }; // quotes needed
No comments:
Post a Comment