How do I create a namespace in JavaScript so that my objects and functions aren't overwritten by other same-named objects and functions? I've used the following:

if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}

Is there a more elegant or succinct way of doing this?

JavaScript does not yet have a native representation of namespaces, but TypeScript does.

For example, you could use the following TS code (playground)

namespace Stack {
    export const hello = () => console.log('hi')
}

Stack.hello()

If you can't update your code to TS, you can at least use the pattern employed by TS when generating the JS output for namespaces, which looks like this:

var Stack;
(function (Stack) {
    Stack.hello = () => console.log('hi');
})(Stack || (Stack = {}));
Stack.hello();

Further Reading: