I am learning about Objects(Dot and Bracket Notations) in JS. I wrote a program that looks up for the ‘value I provided’, in the Object’s list.
Here is the code with Dot Notation:
function lookUp(val) {
var result = "";
var myDog = {
"name": "Anas",
"tails": "4",
"bark": "wwof!",
"breed": "german"
};
result = myDog.val;
return result;
}
console.log(lookUp("name"));
The Output of the above program is: Undefined
Here is the same code with Bracket Notation:
function lookUp(val) {
var result = "";
var lookingUp = {
"name": "Anas",
"tails": "4",
"bark": "wwof!",
"breed": "german"
};
result = lookingUp[val];
return result;
}
console.log(lookUp('name'));
The Output of the above program is: “Anas” //(The result I wanted)
Why I’m not getting the same output(“Anas”) with Dot Notation?