JavaScript Object-Basics - Questionnaire
1 .Why is the last property be followed by a comma in JavaScript ?
The last property in JavaScript can end with a comma (,).
This can be called a "trailing" or "hanging" comma.
Makes it easier to add/remove/move around properties , because all lines become alike.
2. What are the different ways to create an object in JavaScript ?
let user = new Object();
let user = {};
3. What are the different way to access a property in object in JavaScript?
Properties are access with dot(.) and square brackets ([ ]).
Dot Access : user.name
Square Bracket : user["is Employed"]
4. How can we add a multiword property in JavaScript ?
Multi word can be used for property names but must be quoted.
5. How to delete a property from an object in JavaScript with multi word and simple word property name?
Multi-Word : delete user["is Employed"]
Simple-Word : delete user.name
6. What are the restrictions on multi word property in JavaScript ?
Multi word properties cannot be accessed with dot (.)
Using dot(.) to access a multiword return an syntax error.
7. What are computed properties in JavaScript ?
While creating a object literal we use square brackets . Those properties are called computed properties.
The meaning of the computed property is simple [ageInNumber] means that the property name should be taken from ageInNumber i.e. a new property Age is added into the user object.
We can use more complex expressions inside the brackets.
8. What is the output for the snippet below ?
This would return "Avinash Bobba", there is no limitations on property names . They can be an symbols or string.
8. What is the way to do property existence test in JavaScript?
There will be no error error if the accessed property doesn't exist in the object . There are to ways to check a property existence.
Dot : user.noSuchProperty === undefined
In : "name" in user
9. Do we get all properties in the same order they were added ?
The integer properties are sorted and others appear in a creation order.
10 . Do we have a way to iterate over a object ?
To iterate over properties in an object we use for loop.
Ex : for(let key in user)
Comments
Post a Comment