I am trying to print out "John is 31 Years Old" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am trying to print out "John is 31 Years Old"

When i use the pre defined object inside a string it converted into string rather than fetching its value from the Defined Variable. Previously the code was var person = { name: "John", age: 31, favColor: "green", height: 183 }; var x = person.age; var y = person['age']; and i edited it to var person = { name: "John", age: 31, favColor: "green", height: 183 }; var x = person.age; var y = person['age']; document.write("john is" +'x' + "years old"); document.write(y); But cant obtain my desired result which is "John is 31 Years Old" instead i am getting "john isxyears old31" as a result

1st Jan 2018, 3:19 AM
Rahul
2 Answers
+ 8
We wrap strings in single/double quotes in Javascript so 'x' represents a string with a single character. However what you want to achieve is to use/print the value of variable named x, so remove the quotes around it like what you did for the immediate next statement for y and you'll be fine. 😉
1st Jan 2018, 3:23 AM
Zephyr Koo
Zephyr Koo - avatar
+ 1
thank you both 😅
4th Jan 2018, 2:24 AM
Rahul