+ 1

why var name object return undefined?

var name = { user = "username", age = 22 }; var x = name.user; document.write(x); return undefined

12th Feb 2017, 7:50 AM
bacchus a
bacchus a - avatar
1 Odpowiedź
+ 2
Because "name" is a JavaScript Reserved Words, and use a colon (:) instead of the (=) between the user (variables) and "usernames (values) same goes for age Test this out: <!DOCTYPE html> <html> <body> <p id="demo">Display the result here.</p> <script> var person = { user: "usernames", age: 22 }; document.getElementById("demo").innerHTML = person.user; </script> </body> </html> // output: usernames
12th Feb 2017, 11:35 AM
totoro
totoro - avatar