Object Declaration in JavaScript

Used to declare an object, which contains key-value pairs.

var objectName = { key: value };
objectName.key; //accessing the value associated with the key
objectName["key"]; //accessing if the key is provided as a string


Accessing a key that is not set will return undefined.

Values associated with keys can be functions, arrays, or just regular variables.

var legs = { dog: '4', "cat": '4', human: '2' };
legs.dog; //will return 4
legs["cat"]; //cat is declared as a string, access it like this

< Function Assignment   |   Methods >

© 2019 SyntaxDB. All Rights Reserved.