I want to cover a basic but essential part of what you might expect in an introduction to JavaScript. This post is only Part I of fuller explanation of JavaScript function scoping, “lexical scoping,” and Javascript’s hoisting behavior.

Although it may seem tedious to review fundamental elements of JavaScript, these are the building blocks for a comprehensive understanding of the language’s functional style, which I will get to in Parts 2 and 3 of this introduction.

In Part 1 I will cover two basic concepts that are the first things you should learn about JavaScript: “the JavaScript interpreter,” what an identifier is, and the var keyword. Although seemingly unrelated, these two pieces are the foundation of the next parts so I’ve chosen to cover both here.

First, let’s discuss what we mean when we say “the JavaScript interpreter.” This is a standard programming concept, particularly in an interpreted language like Perl, Ruby, PHP, etc, but it is essential to know what the interpreter is to discuss some advanced concepts in JS.

First, consider an extremely simple set of lines of code:

a = 5;
b = 3;
alert(“a plus b is ” + (a+b));

ECMAScript defines a specific standard for the language, and we can expect its syntax to follow a logical relationship to its functioning. However, that’s only part of the story. In the code above we are setting the variable a to 5, the variable b to 3, and then putting up an alert dialog with the sum of the two variables.

The interpreter is the part of the browser’s execution of our code that reads the code we’ve written before it is executed. It parses what we’ve written, figuring out the delimitations between our lines of code and blocks defined with curly braces { and }. Note that it parses our code first, before it actually executes it, and if it can’t parse it correctly we see a Syntax Error in the browser’s console log.

What the interpreter does next is something akin to re-writing certain parts of our code. It doesn’t actually rewrite our code, it just interprets it in a certain way what makes it behave as if it has been re-written. During interpretation, it use what it is interpreting to reserve namespaces for variables and functions, even if it might not actually execute that line of code.

Finally, the code is executed.

Let’s start with a definition of a JavaScript identifier is. An identifier is a string of characters seen by the JS interpreter that will be used as a name something such as a variable or a function.

The term ‘identifier’ just means a string of characters, without spaces. A valid identifer can only contain the letters and numbers (uppercase or lowercase), underscore character _ or dollar-sign $ character. Identifiers must begin with a letter, underscore, or dollar sign. (Digits are not allowed as the first character so that the JavaScript interpreter can recognize number literals quickly.)

These are examples of valid identifiers in Javascript


By Jason

Leave a Reply

Your email address will not be published. Required fields are marked *