0

I am a bit of new to Javascript and I had a bit of confusion. Now for a Javascript object as the one below:

var obj = {
    value:null
    };
obj[value] = 2;

The above code would give me an error at line 2.

But if replace it as below

obj.value = 2;

it would work. Can some one tell me the issue behind why this happens.

user2314737
  • 27,088
  • 20
  • 102
  • 114

2 Answers2

0

You should use obj["value"] = 2 like below

var obj = { value: null };

obj["value"] = 2;

console.log(obj);
Nhan
  • 3,595
  • 6
  • 30
  • 38
Sandeep Nayak
  • 4,649
  • 1
  • 22
  • 33
0

You need to place "" around the value.

var obj = {
     value:null
     };
obj["value"] = 2;