"Namespace" is only used in the JavaScript specification in relation to the module namespace exotic object, which is an object created (or reused) when you do an import * from a module. It contains properties for all of the module's named exports (and a property called default if it has a default export).
Before JavaScript had official modules, "namespace" wasn't used in the definition of JavaScript at all. It was used informally to refer to an object that was created by a unit of code (loosely, a "module") with properties for the "exports" of that module, like this:
// ES5 code - one style of the "revealing module pattern"
var MyLib = (function() {
function internalFunction() {
// ...
}
return {
exportedFunction: function() {
// ...
}
};
})();
There, MyLib was sometimes called a "namespace," but that was purely informal. It's just an object.
"Scope" is a region of program source code that defines a container for variables and related things. (Sometimes it's used to refer to the resulting "objects" defined by the specification, but they're more properly called lexical environment records.) For example, this source code has two explicit scopes:
function example(someParam) {
if (someParam === "some value") {
console.log("something");
}
}
The scopes are:
- Function scope within the
{} defining the function body.
- Block scope within the
{} defining the block on the if.
(There's also the implicit scope around the function, which depends on where this source code appears — sometimes called the "ambient scope.")
At runtime, when example is called, the specification describes creating an environment record for the function scope and later creating an environment record for the block scope. (That's just specification language; a JavaScript engine doesn't have to do it literally.) Sometimes, a scope can have two environment records defined for it (global scope is like that) but usually it's one-for-one.