Which of the following type of variable is visible everywhere in your code?

Skip to main content

Introduction to Python

Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.

Intermediate Python

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Related

Text Data In Python Cheat Sheet

Welcome to our cheat sheet for working with text data in Python! We've compiled a list of the most useful functions and packages for cleaning, processing, and analyzing text data in Python, along with clear examples and explanations, so you'll have everything you need to know about working with text data in Python all in one place.

Python Sets and Set Theory Tutorial

Learn about Python sets: what they are, how to create them, when to use them, built-in functions and their relationship to set theory operations.

Pandas Tutorial: DataFrames in Python

Explore data analysis with Python. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.

See MoreSee More

A variable is a name associated with a value; we say that the variable stores or contains the value. Variables allow you to store and manipulate data in your programs. For example, the following line of JavaScript assigns the value

var sum = i + 3;
7 to a variable named
var sum = i + 3;
8:

i = 2;

And the following line adds

var sum = i + 3;
9 to
var sum = i + 3;
8 and assigns the result to a new variable,
i = 10;
i = "ten";
1:

var sum = i + 3;

These two lines of code demonstrate just about everything you need to know about variables. However, to fully understand how variables work in JavaScript, you need to master a few more concepts. Unfortunately, these concepts require more than a couple of lines of code to explain! The rest of this chapter explains the typing, declaration, scope, contents, and resolution of variables. It also explores garbage collection and the variable/property duality.[]

An important difference between JavaScript and languages such as Java and C is that JavaScript is untyped. This means, in part, that a JavaScript variable can hold a value of any datatype, unlike a Java or C variable, which can hold only the one particular type of data for which it is declared. For example, it is perfectly legal in JavaScript to assign a number to a variable and then later assign a string to that variable:

i = 10;
i = "ten";

In C, C++, Java, or any other strongly typed language, code like this is illegal.

A feature related to JavaScript’s lack of typing is that the language conveniently and automatically converts values from one type to another, as necessary. If you attempt to append a number to a string, for example, JavaScript automatically converts the number to the corresponding string so that it can be appended. Datatype conversion is covered in detail in Chapter 3.

JavaScript is obviously a simpler language for being untyped. The advantage of strongly typed languages such as C++ and Java is that they enforce rigorous programming practices, which makes it easier to write, maintain, and reuse long, complex programs. Since many JavaScript programs are shorter scripts, this rigor is not necessary, and we benefit from the simpler syntax.

Before you use a variable in a JavaScript program, you must declare it.[] Variables are declared with the

i = 10;
i = "ten";
2 keyword, like this:

var i;
var sum;

You can also declare multiple variables with the same

i = 10;
i = "ten";
2 keyword:

var i, sum;

And you can combine variable declaration with variable initialization:

var message = "hello";
var i = 0, j = 0, k = 0;

If you don’t specify an initial value for a variable with the

i = 10;
i = "ten";
2 statement, the variable is declared, but its initial value is
i = 10;
i = "ten";
5 until your code stores a value into it.

Note that the

i = 10;
i = "ten";
2 statement can also appear as part of the
i = 10;
i = "ten";
7 and
i = 10;
i = "ten";
8 loops (introduced in Chapter 6), allowing you to succinctly declare the loop variable as part of the loop syntax itself. For example:

for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");

Variables declared with

i = 10;
i = "ten";
2 are permanent: attempting to delete them with the
var i;
var sum;
0 operator causes an error. (The
var i;
var sum;
0 operator is introduced in Chapter 5.)

Repeated and Omitted Declarations

It is legal and harmless to declare a variable more than once with the

i = 10;
i = "ten";
2 statement. If the repeated declaration has an initializer, it acts as if it were simply an assignment statement.

If you attempt to read the value of an undeclared variable, JavaScript generates an error. If you assign a value to a variable that you have not declared with

i = 10;
i = "ten";
2, JavaScript implicitly declares that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function. To prevent the creation of a global variable (or the use of an existing global variable) when you meant to create a local variable to use within a single function, you must always use the
i = 10;
i = "ten";
2 statement within function bodies. It’s best to use
i = 10;
i = "ten";
2 for all variables, whether global or local. (The distinction between local and global variables is explored in more detail in the next section.)

The scope of a variable is the region of your program in which it is defined. A global variable has global scope; it is defined everywhere in your JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.

Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. For example, the following code prints the word “local”:

var scope = "global";         // Declare a global variable
function checkscope( ) {
    var scope = "local";      // Declare a local variable with the same name
    document.write(scope);    // Use the local variable, not the global one
}
checkscope( );                 // Prints "local"

Although you can get away with not using the

i = 10;
i = "ten";
2 statement when you write code in the global scope, you must always use
i = 10;
i = "ten";
2 to declare local variables. Consider what happens if you don’t:

scope = "global";             // Declare a global variable, even without var
function checkscope( ) {
    scope = "local";          // Oops! We just changed the global variable
    document.write(scope);    // Uses the global variable
    myscope = "local";        // This implicitly declares a new global variable
    document.write(myscope);  // Uses the new global variable
}
checkscope( );                 // Prints "locallocal"
document.write(scope);        // This prints "local"
document.write(myscope);      // This prints "local"

In general, functions do not know what variables are defined in the global scope or what they are being used for. Thus, if a function uses a global variable instead of a local one, it runs the risk of changing a value on which some other part of the program relies. Fortunately, avoiding this problem is simple: declare all variables with

i = 10;
i = "ten";
2.

Function definitions can be nested. Each function has its own local scope, so it is possible to have several nested layers of local scope. For example:

var scope = "global scope";          // A global variable
function checkscope( ) {
    var scope = "local scope";       // A local variable
    function nested( ) {
        var scope = "nested scope";  // A nested scope of local variables
        document.write(scope);       // Prints "nested scope"
    }
    nested( );
}
checkscope( );

Note that unlike C, C++, and Java, JavaScript does not have block-level scope. All variables declared in a function, no matter where they are declared, are defined throughout the function. In the following code, the variables

var sum = i + 3;
8,
var i, sum;
0, and
var i, sum;
1 all have the same scope: all three are defined throughout the body of the function. This would not be the case if the code were written in C, C++, or Java:

var sum = i + 3;
0

The rule that all variables declared in a function are defined throughout the function can cause surprising results. The following code illustrates this:

var sum = i + 3;
1

You might think that the first call to

var i, sum;
2 would display “global”, because the
i = 10;
i = "ten";
2 statement declaring the local variable has not yet been executed. Because of the scope rules, however, this is not what happens. The local variable is defined throughout the body of the function, which means the global variable by the same name is hidden throughout the function. Although the local variable is defined throughout, it is not actually initialized until the
i = 10;
i = "ten";
2 statement is executed. Thus, the function
var i, sum;
5 in the previous example is equivalent to the following:

var sum = i + 3;
2

This example illustrates why it is good programming practice to place all your variable declarations together at the start of any function.

Undefined Versus Unassigned

The examples in the previous section demonstrate a subtle point in JavaScript programming: there are two different kinds of undefined variables. The first kind of undefined variable is one that has never been declared. An attempt to read the value of such an undeclared variable causes a runtime error. Undeclared variables are undefined because they simply do not exist. As described earlier, assigning a value to an undeclared variable does not cause an error; instead, it implicitly declares the variable in the global scope.

The second kind of undefined variable is one that has been declared but has never had a value assigned to it. If you read the value of one of these variables, you obtain its default value,

i = 10;
i = "ten";
5. This type of undefined variable might more usefully be called unassigned, to distinguish it from the more serious kind of undefined variable that has not even been declared and does not exist.

The following code fragment illustrates some of the differences between truly undefined and merely unassigned variables:

var sum = i + 3;
3

Primitive Types and Reference Types

The next topic we need to consider is the content of variables. We often say that variables have or contain values. But just what is it that they contain? To answer this seemingly simple question, we must look again at the datatypes supported by JavaScript. The types can be divided into two groups: primitive types and reference types. Numbers, boolean values, and the

var i, sum;
7 and
i = 10;
i = "ten";
5 types are primitive. Objects, arrays, and functions are reference types.

A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types. If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.[]

Reference types are another matter, however. Objects, for example, can be of any length: they do not have a fixed size. The same is true of arrays: an array can have any number of elements. Similarly, a function can contain any amount of JavaScript code. Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable. Instead, the variable stores a reference to the value. Typically, this reference is some form of pointer or memory address. It is not the data value itself, but it tells the variable where to look to find the value.

The distinction between primitive and reference types is an important one because they behave differently. Consider the following code that uses numbers (a primitive type):

var sum = i + 3;
4

There is nothing surprising about this code. Now consider what happens if we change the code slightly so that it uses arrays (a reference type) instead of numbers:

var sum = i + 3;
5

If this result does not seem surprising to you, you’re already well familiar with the distinction between primitive and reference types. If it does seem surprising, take a closer look at the second line. Note that it is the reference to the array value, not the array itself, that is being assigned in this statement. After that second line of code, we still have only one array object; there just happens to be two references to it.

If the primitive-versus-reference type distinction is new to you, just try to keep the variable contents in mind. Variables hold the actual values of primitive types, but they hold only references to the values of reference types. The differing behavior of primitive and reference types is explored in more detail in .

You may have noticed that I did not specify whether strings are primitive or reference types in JavaScript. Strings are an unusual case. They have variable sizes, so obviously they cannot be stored directly in fixed-size variables. For efficiency, you would expect JavaScript to copy references to strings, not the actual contents of strings. On the other hand, strings behave like a primitive type in many ways. The question of whether strings are a primitive or reference type is actually moot because strings are immutable: there is no way to change the contents of a string value. This means that you cannot construct an example like the previous one that demonstrates that arrays are copied by reference. In the end, it doesn’t matter much whether you think of strings as an immutable reference type that behaves like a primitive type or as a primitive type implemented with the internal efficiency of a reference type.

Reference types do not have a fixed size; indeed, some of them can become quite large. As we’ve already discussed, variables do not directly hold reference values. The value is stored at some other location, and the variables merely hold a reference to that location. Now let’s focus briefly on the actual storage of the value.

Since strings, objects, and arrays do not have a fixed size, storage for them must be allocated dynamically, when the size is known. Every time a JavaScript program creates a string, array, or object, the interpreter must allocate memory to store that entity. Whenever memory is dynamically allocated like this, it must eventually be freed up for reuse, or the JavaScript interpreter will use up all the available memory on the system and crash.

In languages such as C and C++, memory must be freed manually. It is the programmer’s responsibility to keep track of all objects that are created and to destroy them (freeing their memory) when they are no longer needed. This can be an onerous task and is often the source of bugs.

Instead of requiring manual deallocation, JavaScript relies on a technique called garbage collection. The JavaScript interpreter can detect when an object will never again be used by the program. When it determines that an object is unreachable (i.e., there is no longer any way to refer to it using the variables in the program), it knows that the object is no longer needed and its memory can be reclaimed. Consider the following lines of code, for example:

var sum = i + 3;
6

After this code runs, the original string “hello” is no longer reachable; there are no references to it in any variables in the program. The system detects this fact and frees up its storage space for reuse.

Garbage collection is automatic and is invisible to the programmer. You need to know only enough about garbage collection to trust that it works: to know you can create all the garbage objects you want, and the system will clean up after you!

You may have noticed by now that there are a lot of similarities in JavaScript between variables and the properties of objects. They are both assigned the same way, they are used the same way in JavaScript expressions, and so on. Is there really any fundamental difference between the variable

var sum = i + 3;
8 and the property
var sum = i + 3;
8 of an object
var message = "hello";
var i = 0, j = 0, k = 0;
1? The answer is no. Variables in JavaScript are fundamentally the same as object properties.

When the JavaScript interpreter starts up, one of the first things it does, before executing any JavaScript code, is create a global object. The properties of this object are the global variables of JavaScript programs. When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object.

The JavaScript interpreter initializes the global object with a number of properties that refer to predefined values and functions. For example, the

var message = "hello";
var i = 0, j = 0, k = 0;
2,
var message = "hello";
var i = 0, j = 0, k = 0;
3, and
var message = "hello";
var i = 0, j = 0, k = 0;
4 properties refer to the number infinity, the predefined
var message = "hello";
var i = 0, j = 0, k = 0;
5 function, and the predefined Math object, respectively. You can read about these global values in Part III.

In top-level code (i.e., JavaScript code that is not part of a function), you can use the JavaScript keyword

var message = "hello";
var i = 0, j = 0, k = 0;
6 to refer to the global object. Within functions,
var message = "hello";
var i = 0, j = 0, k = 0;
6 has a different use, which is described in Chapter 8.

In client-side JavaScript, the Window object serves as the global object for all JavaScript code contained in the browser window it represents. This global Window object has a self-referential

var message = "hello";
var i = 0, j = 0, k = 0;
8 property that can be used instead of
var message = "hello";
var i = 0, j = 0, k = 0;
6 to refer to the global object. The Window object defines the core global properties, such as
var message = "hello";
var i = 0, j = 0, k = 0;
3 and
var message = "hello";
var i = 0, j = 0, k = 0;
4, and also global client-side properties, such as
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
2 and
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
3.

Local Variables: The Call Object

If global variables are properties of the special global object, then what are local variables? They too are properties of an object. This object is known as the call object. The call object has a shorter lifespan than the global object, but it serves the same purpose. While the body of a function is executing, the function arguments and local variables are stored as properties of this call object. The use of an entirely separate object for local variables is what allows JavaScript to keep local variables from overwriting the value of global variables with the same name.

JavaScript Execution Contexts

Each time the JavaScript interpreter begins to execute a function, it creates a new execution context for that function. An execution context is, obviously, the context in which any piece of JavaScript code executes. An important part of the context is the object in which variables are defined. Thus, JavaScript code that is not part of any function runs in an execution context that uses the global object for variable definitions. And every JavaScript function runs in its own unique execution context with its own call object in which local variables are defined.

An interesting point to note is that JavaScript implementations may allow multiple global execution contexts, each with a different global object.[] (Although, in this case, each global object is not entirely global.) The obvious example is client-side JavaScript, in which each separate browser window, or each frame within a window, defines a separate global execution context. Client-side JavaScript code in each frame or window runs in its own execution context and has its own global object. However, these separate client-side global objects have properties that link them. Thus, JavaScript code in one frame might refer to another frame with the expression

for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
4, and the global variable
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
5 in the first frame might be referenced by the expression
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
6 in the second frame.

You don’t need to fully understand just yet how separate window and frame execution contexts are linked together in client-side JavaScript. That topic is covered in detail in the discussion on the integration of JavaScript with web browsers in Chapter 13. What you should understand now is that JavaScript is flexible enough that a single JavaScript interpreter can run scripts in different global execution contexts and that those contexts need not be entirely separate; they can refer back and forth to each other.

This last point requires additional consideration. When JavaScript code in one execution context can read and write property values and execute functions that are defined in another execution context, you’ve reached a level of complexity that requires consideration of security issues. Take client-side JavaScript as an example. Suppose browser window A is running a script or contains information from your local intranet, and window B is running a script from some random site out on the Internet. In general, you do not want to allow the code in window B to be able to access the properties of window A. If you do allow it to do so, window B might be able to read sensitive company information and steal it, for example. Thus, in order to safely run JavaScript code, there must be a security mechanism that prevents access from one execution context to another when such access should not be permitted. We’ll return to this topic in .

When we first discussed the notion of variable scope, I based the definition solely on the lexical structure of JavaScript code: global variables have global scope, and variables declared in functions have local scope. If one function definition is nested within another, variables declared within that nested function have a nested local scope. Now that you know that global variables are properties of a global object and that local variables are properties of a special call object, we can return to the notion of variable scope and reconceptualize it. This new description of scope offers a useful way to think about variables in many contexts; it provides a powerful new understanding of how JavaScript works.

Every JavaScript execution context has a scope chain associated with it. This scope chain is a list or chain of objects. When JavaScript code needs to look up the value of a variable

for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
5 (a process called variable name resolution), it starts by looking at the first object in the chain. If that object has a property named
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
5, the value of that property is used. If the first object does not have a property named
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
5, JavaScript continues the search with the next object in the chain. If the second object does not have a property named
for(var i = 0; i < 10; i++) document.write(i, ">br<");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, ">br<");
for(var i in o) document.write(i, ">br<");
5, the search moves on to the next object, and so on.

In top-level JavaScript code (i.e., code not contained within any function definitions), the scope chain consists of a single object, the global object. All variables are looked up in this object. If a variable does not exist, the variable value is undefined. In a (nonnested) function, however, the scope chain consists of two objects. The first is the function’s call object, and the second is the global object. When the function refers to a variable, the call object (the local scope) is checked first, and the global object (the global scope) is checked second. A nested function would have three or more objects in its scope chain. illustrates the process of looking up a variable name in the scope chain of a function.

Which of the following type of variable is visible everywhere in your PHP code?

Built-in superglobal variables are visible everywhere within a script.

What is implicit variable in JavaScript?

If you assign a value to a variable that you have not declared with var , JavaScript implicitly declares that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function.

Which of the following type of variable takes precedence?

A local variable takes precedence over a global variable with the same name.

Which of the following is the correct way to create a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().