Articles → JavaScript → Javascript Object Literal
Javascript Object Literal
Definition
Syntax
{Key1:value1, key2:value2}
Example
var test = {Name:'Karan', Address:'India'}
How To Access Values?
var test = {Name:'Karan', Address:'India'}
alert(test.Name);
alert(test.Address);
Types Of Values
- Any data type
- Array
- Function
- Nested key-value pair
var test = {
Name: 'Karan',
Age: 32,
Books: ['Book1', 'Book2'],
Height: {
feet: 5,
inch: 4
},
Click: function() {
alert('click me')
}
};
alert(test.Name);
alert(test.Age);
alert(test.Books[0]);
alert(test.Books[1]);
alert(test.Height.feet);
alert(test.Height.inch);
test.Click();