1

hi everyone . actually I'm newly learning Javascript . I read about scopes in javascript . then I read somewhere about namespace in Js and i was wondered if namespace is exactly the same as scope , so i searched but the results were just explained for python and I don't know if they are the same in js or not .

would someone explain the difference between scope and namespace in js ?

my definition of scope : scope is a concept in programming language that helps us to prevent variable pollution . it means we control the accessibility to the variables and function in our code . js is a function scope language it means new scopes will create if we create new function . we can declare block scope instead of function scope variable and function with the help of new keywords let , const

7
  • 3
    There is nothing officially called "namespace" in JavaScript. There is an official concept called "scope". So, I guess the difference is that one exists, the other doesn't. Commented Aug 24, 2021 at 5:48
  • 3
    @VLAZ - Module Namespace Exotic Objects. Commented Aug 24, 2021 at 5:51
  • 2
    @T.J.Crowder OK, I stand corrected. But I feel OP isn't talking about module namespace exotic objects specifically. Commented Aug 24, 2021 at 5:53
  • 2
    "...I read somewhere about namespace in Js" - where did you read it? Can you share the link? Commented Aug 24, 2021 at 5:58
  • 1
    A short and simple description Commented Aug 24, 2021 at 6:13

1 Answer 1

4

"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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for your answer T.J. Crowder . you know , I'm very beginner in JS and your i found some concept in your explanation that I haven't read about them . so i think its better to finish the entire course and then again read yours .
@mahdizarepoor - Fair enough and I hope your learning goes well! BTW, your definition of "scope" isn't bad at all. It's not just isolating variables, but what you have is really not a bad starting point for understanding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.