Skip to content

Instantly share code, notes, and snippets.

@saifsmailbox98
Last active June 28, 2017 21:11
Show Gist options
  • Save saifsmailbox98/d4b09e2e6eb768abb6e44934a45f208a to your computer and use it in GitHub Desktop.
Save saifsmailbox98/d4b09e2e6eb768abb6e44934a45f208a to your computer and use it in GitHub Desktop.
var obj = {

"name" : "Saif",
"username" : "saifsmailbox98"

}; //object
var name = "username";  //variable name contains a string "username"
obj.name; // "Saif"
obj["name"]; // "Saif"
obj[name]; // "saifsmailbox98"
obj.username; // "saifsmailbox98"
obj["username"]; // "saifsmailbox98"
obj[username]; // ReferenceError: username is not defined 
  1. looks for the property named name and returns it's value (variable name not used here).
  2. same as above.
  3. looks for the property that the variable name contains that is "username" (variable name used here).
  4. looks for the property named username and returns it's value (variable name not used here).
  5. same as above.
  6. looks for the property that the variable username contains (variable username does not exist).

💬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment