Skip to content
Advertisement

Not sure why the method is not returning any value

I know I already have a similar post, but I can’t for the life of me work out what is different between the two snippets – as I can only get the return value from the first example – but not the second example; both methods in the examples return a value.

What is even more bizarre (or frustrating) is that the PHP interpreter does not throw up any errors on the second example; it simply presents a blank screen. I am running on XAMPP and I believe “show errors”, etc. is set to true.

First example (works perfectly):

JavaScript

Second example (note only the ‘myMethod’ function has changed):

JavaScript

Further to what the system said, it looks like the variable contains the correct data, but “document.write” does not seem to pipe it out to the browser view properly:

JavaScript

Advertisement

Answer

Have a look at the source code view of your browser. The script seems to work as you may expect (see below).

But note that in JavaScript it is not possible to build multiline strings this way.

When building a multiline string in JavaScript you’ll have to do this:

JavaScript

Or use the join function:

JavaScript

Here comes your output:

JavaScript
Advertisement