JavaScript Values, Names, and Literals


JavaScript recognizes the following types of values:

This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. Notice that there is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in Navigateur. However, the date object and related built-in functions enable you to handle dates.

Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.

Datatype Conversion

JavaScript is a loosely typed language. That means that you do not have to specify the datatype of a variable when you declare it, and datatypes are converted automatically as needed during the course of script execution. So, for example, you could define a variable as follows:

var answer = 42

And later, you could assign the same variable a string value, for example:

answer = "Thanks for all the fish..."

Because JavaScript is loosely typed, this will not cause an error message.

In general, in expressions involving numeric and string values, JavaScript converts the numeric values to strings. For example, consider the following statements:

x = "The answer is " + 42
y = 42 + " is the answer."

The first statement will the string "The answer is 42". The second statement will return the string "42 is the answer".

JavaScript provides several special functions for manipulating string and numeric values:

Variable Names

You use variables to hold values in your application. You give these variables names by which you reference them, and there are certain rules to which the names must conform.

A JavaScript identifier or name must start with a letter or underscore ("_"); subsequent characters can also be digits (0-9). Letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). JavaScript is case-sensitive.

Some examples of legal names are:


Literals are the way you represent values in JavaScript. These are fixed values that you literally provide in your application source, and are not variables. Examples of literals include:


Integers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) format. A decimal integer literal consists of a sequence of digits (optionally suffixed as described below) without a leading 0 (zero).

An integer can be expressed in octal or hexadecimal rather than decimal. A leading 0 (zero) on an integer literal means it is in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.

Floating Point Literals

A floating point literal can have the following parts: a decimal integer, a decimal point ("."), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by a "+" or "-"). A floating point literal must have at least one digit, plus either a decimal point or "e" (or "E"). Some examples of floating point literals are:

Boolean Literals

The boolean type has two literal values: true and false.

String Literals

A string literal is zero or more characters enclosed in double (") or single (') quotes. A string must be delimited by quotes of the same type; that is, either both single quotes or double quotes. The following are examples of string literals:

Special Characters

You can use the following special characters in JavaScript string literals:

Escaping Characters

You can insert quotes inside of strings by preceding them by a backslash. This is known as escaping the quotes. For example,

var quote = "

He read \"The Cremation of Sam McGee\" by R.W. Service" document.write(quote)

The result of this would be