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.
Description
Literals
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:
42
3.1245
"To be or not to be"
Integers3.1245
"To be or not to be"
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.
JavaScript: Floating number literals
A floating number has the following parts.
A decimal integer.
A decimal point ('.').
A fraction.
An exponent.
A decimal point ('.').
A fraction.
An exponent.
The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by "+" or "-").
Example of some floating numbers:
8.2935
-14.72
12.4e3
4E-3 [ Equivalent to 4 x 10-3 =>.004 ]
Boolean Literals-14.72
12.4e3
4E-3 [ Equivalent to 4 x 10-3 =>.004 ]
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:
"blah"
"hey"
"123456"
"one line \n another line"
Special Characters"hey"
"123456"
"one line \n another line"
You can use the following special characters in JavaScript string literals:
\b indicates a backspace.
\f indicates a form feed.
\n indicates a new line character.
\r indicates a carriage return.
\t indicates a tab character.
Escaping Characters\f indicates a form feed.
\n indicates a new line character.
\r indicates a carriage return.
\t indicates a tab character.
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 \"Next Generation \" by C.W. Faraday"
document.write(quote)
var quote = "He read \"Next Generation \" by C.W. Faraday"
document.write(quote)
The result of this would be
He read " Next Generation " by C.W. Faraday.
No comments:
Post a Comment