Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

Understanding JavaScript Syntax

Like any other computer language, JavaScript is based on a rigid syntax where specific words mean different things to the browser as it interprets the script. This section is designed to walk you through the basics of creating variables, working with data types, and using looping and functions in JavaScript to manipulate your web pages.

Creating Variables

The first place to begin with in JavaScript is variables. Variables are a means to name data so that you can use that name to temporarily store and access data from your JavaScript files. Variables can point to simple data types, such as numbers or strings, or they can point to more complex data types, such as objects.

To define a variable in JavaScript, you must use the var keyword and then give the variable a name; for example:

var myData;

You can also assign a value to the variable in the same line. For example, the following line of code creates a variable myString and assigns it the value of "Some Text":

var myString = "Some Text";

This works as well as

var myString;
myString = "Some Text";

After you have declared the variable, you can use the name to assign the variable a value and access the value of the variable. For example, the following code stores a string into the myString variable and then uses it when assigning the value to the newString variable:

var myString = "Some Text";
var newString = myString + "Some More Text";

Your variable names should describe the data that is stored in them so that it is easy to use them later in your program. The only rule for creating variable names is that they must begin with a letter, $, or _, and they cannot contain spaces. Also remember that variable names are case sensitive, so using myString is different from MyString.

Understanding JavaScript Data Types

JavaScript uses data types to determine how to handle data that is assigned to a variable. The variable type will determine what operations you can perform on the variable, such as looping or executing. The following list describes the most common types of variables that we will be working with through the book:

  • String—Stores character data as a string. The character data is specified by either single or double quotes. All the data contained in the quotes will be assigned to the string variable. For example:
  • var myString = 'Some Text';
    var anotherString = “Some Other Text";
  • Number—Stores the data as a numerical value. Numbers are useful in counting, calculations, and comparisons. Some examples are
  • var myInteger = 1;
    var cost = 1.33;
  • Boolean—Stores a single bit that is either true or false. Booleans are often used for flags. For example, you might set a variable to false at the beginning of some code and then check it on completion to see whether the code execution hit a certain spot. The following shows an example of defining a true and a false variable:
  • var yes = true;
    var no = false;
  • Array—An indexed array is a series of separate distinct data items all stored under a single variable name. Items in the array can be accessed by their zero-based index using the [index]. The following is an example of creating a simple array and then accessing the first element, which is at index 0:
  • var arr = ["one", "two", "three"]
    var first = arr[0];
  • Associative Array/Objects—JavaScript does support the concept of an associative array, meaning accessing the items in the array by a name instead of an index value. However, a better method is to use an object literal. When you use an object literal, you can access items in the object using object.property syntax. The following example shows how to create and access an object literal:
  • var obj = {"name":"Brad", "occupation":"Hacker", "age", "Unknown"};
    var name = obj.name;
  • Null—At times you do not have a value to store in a variable, either because it hasn’t been created or you are no longer using it. At this time you can set a variable to null. That way you can check the value of the variable in your code and use it only if it is not null.
  • var newVar = null;

Using Operators

JavaScript operators provide the capability to alter the value of a variable. You are already familiar with the = operator because you used it several times in the book already. JavaScript provides several operators that can be grouped into two types—arithmetic and assignment.

Arithmetic Operators

Arithmetic operators are used to perform operations between variable and direct values. Table 5.1 shows a list of the arithmetic operations along with the results that get applied.

Table 5.1 Table Showing JavaScript’s Arithmetic Operators as Well as Results Based on y=4 to Begin With




Resulting x

Resulting y




















Modulous (remainder of Division)


Assignment Operators

Assignment operators are used to assign a value to a variable. You are probably used to the = operator, but there are several forms that allow you to manipulate the data as you assign the value. Table 5.2 shows a list of the assignment operations along with the results that get applied.

Table 5.2 JavaScript’s Assignment Operators as Well as Results Based on x=10 to Begin With



Equivalent Arithmetic Operators

Resulting x


Applying Comparison and Conditional Operators

Conditionals are a way to apply logic to your applications so that certain code will be executed only under the correct conditions. This is done by applying comparison logic to variable values. The following sections describe the comparisons available in JavaScript and how to apply them in conditional statements.

Comparison Operators

A comparison operator evaluates two pieces of data and returns true if the evaluation is correct or false if the evaluation is not correct. Comparison operators compare the value on the left of the operator against the value on the right.

The simplest way to help you understand comparisons is to provide a list with some examples. Table 5.3 shows a list of the comparison operators along with some examples.

Table 5.3 JavaScript’s Comparison Operators as Well as Results Based on x=10 to Begin With






Is equal to (value only)






Both value and type are equal






Is not equal




Both value and type are not equal






Is greater than




Is greater than or equal to




Is less than




Is less than or equal to



You can chain multiple comparisons together using logical operators. Table 5.4 shows a list of the logical operators and how to use them to chain comparisons together.

Table 5.4 JavaScript’s Comparison Operators as Well as Results Based on x=10 and y=5 to Begin With







(x==10 && y==5)

(x==10 && y>x)





(x>=10 || y>x)

(x<10 && y>x)





!(x==y) !(x>y)




(x>=10 && y<x || x==y)

((x<y || x>=10) && y>=5)

(!(x==y) && y>=10)





An if statement enables you to separate code execution based on the evaluation of a comparison. The syntax is shown in the following lines of code where the conditional operators are in () parentheses and the code to execute if the conditional evaluates to true is in {} brackets:


In addition to executing code only within the if statement block, you can specify an else block that will get executed only if the condition is false. For example:

} else {

You can also chain if statements together. To do this, add a conditional statement along with an else statement. For example:

} else if(x<10) {
} else {


Another type of conditional logic is the switch statement. The switch statement allows you to evaluate an expression once and then, based on the value, execute one of many sections of code.

The syntax for the switch statement is the following:

  case value:
    code to execute
  case value2:
    code to execute
    code to execute if not value or value2.

This is what is happening. The switch statement will evaluate the expression entirely and get a value. The value may be a string, a number, a Boolean, or even an object. The switch value is then compared to each value specified by the case statement. If the value matches, the code in the case statement is executed. If no values match, the default code is executed.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers





Collection and Use of Information


Questions and Inquiries


Online Store




Contests and Drawings




Service Announcements


Customer Service


Other Collection and Use of Information

Application and System Logs


Web Analytics


Cookies and Related Technologies


Do Not Track

This site currently does not respond to Do Not Track signals.






Pearson may send or direct marketing communications to users, provided that



Correcting/Updating Personal Information




Sale of Personal Information



Supplemental Privacy Statement for California Residents


Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.



Requests and Contact


Changes to this Privacy Notice


Last Update: November 17, 2020