Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript object expression notation is not working properly [duplicate]

I am new to javascript language. The format is given on web, I tried but it giving the undefined result.

var name = {
  a : 'a',
  b:'b',c:'c'
};
console.log(name.a);// undefined
console.log(name);// '[object object]'

The output is undefined ? why

like image 461
Minion Avatar asked Mar 16 '26 15:03

Minion


1 Answers

You have a conflict with window.name. If you use name in a global context, the value is stringified. The solution is to use the variable only within a function context instead, or anywhere outside of global scope:

var f = function(){
  var name = {
    a : "a",
    b : "b",
    c : "c"
  };
  console.log(name.a);
  console.log(name);
}

f();
like image 82
Karl Reid Avatar answered Mar 18 '26 23:03

Karl Reid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!