DEV Community

kambala yashwanth
kambala yashwanth

Posted on

Obj initialization same key_and_value

var k_as_v = "k_as_v";
obj = {

    [k_as_v]:k_as_v,

}

console.log(obj);

Why cant I just keep k_as_v

var k_as_v ="k_as_v";

obj = {

   [k_as_v],

}

console.log(obj);

If cant how can i achieve this without re-declaring, any hack !

Latest comments (4)

Collapse
 
rhymes profile image
rhymes

Just remove the square brackets.

{ kasv }

Collapse
 
yashwanth2804 profile image
kambala yashwanth

No mate , not working
jsbin.com/nuboloqaqi/edit?js,console

Collapse
 
rhymes profile image
rhymes

One set of curly brackets too many.

Obj = { kasv }

Thread Thread
 
yashwanth2804 profile image
kambala yashwanth

Thank you, Done !