Difference between document.write(funName()) and document.write(funName ()) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between document.write(funName()) and document.write(funName ())

The code below outputs "bar" but if we insert a space between printName and () in first line like document.write(name, printName ()) it will output "foobar". Can someone please explain me what is the meaning of this space here. document.write(name, printName()); var name="foo"; function printName(){ return "bar"; }

18th Jun 2018, 6:28 AM
Pikachu
Pikachu - avatar
3 Answers
+ 1
When you run the code first, in line 1, `name` is undefined, so it will not print. That's because you define `name` in line 2. If you run the same code a second time it works, with space or without, because `name` is set to "foo". I thought I was going nuts for a second, funny bug!
18th Jun 2018, 6:42 AM
Schindlabua
Schindlabua - avatar
0
Space shouldnt change anything, lookup for scope and hoisting
18th Jun 2018, 8:09 AM
damyco
damyco - avatar
0
Thanks for the help i got it now, what i m doing wrong.
19th Jun 2018, 4:12 AM
Pikachu
Pikachu - avatar