Matthew Corrao
Matthew Corrao

Reputation: 9

Unique object literal keys- javascript

var obj = {"":"empty string", "!":"bang", "hello world":"hello world"}

-Fix these lines of code so it works?

Whatever method i've tried, i keep coming up with an undefined property name for "" or an error when calling for !.

I'm very new to coding and am not looking for an answer, maybe just a push in the right direction..So please be gentle and thanks for any help

Upvotes: 0

Views: 72

Answers (2)

Emilio Platzer
Emilio Platzer

Reputation: 2469

You must call obj like an Array

console.log( obj [''] ); // "empty string"
console.log( obj ['!'] ); // "bang"
var x='!';
console.log( obj [x] ); // "bang"

Upvotes: 2

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

As per my understanding you want to retrieve "Empty String"

You can get the information by following

var obj = {"":"empty string", "!":"bang", "hello world":"hello world"}

console.log(obj[""]);

Upvotes: 0

Related Questions