Tag Archives: developers

Variable Cautions and Gotchas in JavaScript

Thumbnail for 688

When it comes to JavaScript, there are a few variable cautions and gotchas to be aware of.

Keep in mind that although the value stored in a JavaScript variable is strongly typed—a string, number, Boolean, or object—the variable can hold any type during program execution. There is no type defined as part of the declaration. So the following code, although convoluted and probably confusing to maintain, is perfectly legitimate. Don’t do this! Use each variable for a single purpose to make your code clearer and give it a name related to that purpose. In general, you should use each variable for a single type, even though JavaScript doesn’t enforce this.

Be careful when splitting a var statement across multiple lines! If you accidentally omit any commas, all of the subsequent variables will be declared as global variables, which pollutes the global namespace and causes hard to find bugs. So, for example, if you omitted the comma after the count variable is declared and initialized, as in the following code, JavaScript injects a semicolon at the end of the first line and the minMsgID and lastName variables will be global variables. (The indenting makes no difference to JavaScript.) This problem is avoided if you have the entire var statement on a single line in the code file or declare each variable in its own statement with the var keyword.

TIP: In ECMAScript 5/Strict mode, it is an error to assign a value to an undeclared variable. But in older versions of the standard, and in ECMAScript 5/Default mode, the variable is declared as a property of the global object as described above.

It is legal and generally harmless to declare a variable more than once with the var statement, although it takes extra typing and clutters up your code. If a later, redundant var statement also initializes the variable, it acts as a simple assignment statement. No matter how many times you declare a variable within its scope, you’ll have only a single instance of the variable.

Get more JavaScript training Today!

Escape Sequences in String Literals Using JavaScript

Thumbnail for 692

One of the fundamental tasks handled by programming languages is to temporarily store and manipulate bits of data in memory, mostly strings and numbers, but often also dates and other special data types, called values. One of the defining characteristics of a programming language is the data types it supports and the features it has for working with those types. A language’s types define the kinds of values you can represent and work with. When you need to save a value for later use in the program, you can define a variable and store the value in it. A variable is a symbolic name for the value that you store in memory, and lets you refer to the value by name and, over the course of program execution, assign different values to the same variable.

Following is an explanation of Escape Sequences in String Literals and their use in JavaScript.

Escape Sequences in String Literals

When you need to include any of a number of characters that have special meaning within a string literal, you can create an escape sequence using a backslash to identify the special character. For example, if you absolutely must include the type of quote used as a string delimiter within the string, you can create an escape sequence as in the following string literal. By making it an escape sequence, the single quote will appear within the string, even though that is also the string delimiter.

The reason why this is called an escape sequence is that the backslash causes an escape from the usual interpretation of the single quote character in the previous example. Table 2 lists the available JavaScript escape sequences.

If you include the backslash in front of any other character than those shown in Table 2, JavaScript will ignore the backslash, such as if you include * in a string literal. This is risky however, because future versions of JavaScript could define new escape sequences that would give meaning to the sequence in your code. As unlikely as that may seem, it’s best to avoid the possibility of broken code in the future.

The following code demonstrates some of these escape sequences. Some lines are broken in random places to fit on the page, but each console.log statement should appear on a single line in a code file. Figure 3 shows the results in the Console panel.


Figure 3. The results in the Console panel of running sample escape sequence code.

Get more JavaScript training Today!