DEV Community

loading...

Obj initialization same key_and_value

kambala yashwanth
I ❤️ JavaScript > any other ☕ logo languages
・1 min read
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 !

Discussion (4)

Collapse
rhymes profile image
rhymes

Just remove the square brackets.

{ kasv }

Collapse
yashwanth2804 profile image
kambala yashwanth Author

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 Author

Thank you, Done !