JavaScript functions always return undefined when you have not explicitly called return. Except in the case when you called the function with new. In that case the function returns this (a reference to the object that was created).
function foo() {
}
foo() // returns undefined
new foo() // returns a reference to the object that was created
Convention dictates that if a function is to be used as a constructor that it is capitalized
function Foo() {
this.x = 5;
// return this; // is implicit
}
This convention is helpful because really bad stuff will happen if you call the function Foo and forget to quantify it with new.
x = 42
Foo() // this mistake is easier to spot if the function is capitalized
// x is now equal to 5.
x = 42
f = new Foo()
// x is still 42 and f.x is 5
Why do the values not go into the array?
A program can not simply print values into its own source code. To do so would be mind bogglingly impossible to reason about. The other answers on this page are both good solutions to your specific problem.
range(x)doesn't return anything.