Santosh
Santosh

Reputation: 885

Difference between a namespace and identifier in javascript

An identifier is simply a name and a namespace is also a name but to a literal.

I just got this question in a discussion forum, but I was not happy with answer (above) given.

Upvotes: 0

Views: 136

Answers (1)

Parth Akbari
Parth Akbari

Reputation: 651

An identifier is simply a name. In JavaScript, identifiers are used to name variables and functions and to provide labels for certain loops in JavaScript code. The rules for legal identifier names are the same in JavaScript as they are in Java and many other languages. The first character must be a letter, an underscore (_), or a dollar sign ($).Subsequent characters may be any letter or digit or an underscore or dollar sign. (Numbers are not allowed as the first character so that JavaScript can easily distinguish identifiers from numbers.) Read more

Namespacing in Javascript is achieved by defining properties on the Global Object, in browsers that is the window object. Every var declaration performed in the global context will create the variable under the window object, making it available globally.Read more

var one = 1;
window.one === one; // true
function fn() {
 1 === one; // true 
 }

Upvotes: 1

Related Questions