| Primitive Types | |
|---|---|
| Data Type | Description |
| Integer numbers | Positive or negative numbers with no decimal places |
| Floating-point numbers | Positive or negative numbers with decimal places or numbers written using exponential notation. Ex: 2.5e3=2.5*10*10*10=2500 |
| Boolean | A logical value of true or false. JavaScript converts the values of true and false to the integers 1 and 0 in a mathematical operation. |
| String | Text such as "Hello World". |
| Undefined | A variable that has never had a value assigned to it, has not been declared, or does not exist |
| Null | An empty value |
The JavaScript language supports primitive data types. Data types that can be assigned only a single value are called primitive data types. It also supports reference, or composite data types, which are collections of data represented by a single variable name. The three reference data types supported by the JavaScript language are functions, objects, and arrays. The collections of data in an object are its methods and properties. Arrays are sets of data represented by a single variable name. The variable name that represents an array is often referred to as an object, or array object.
Programming languages that require you to declare the data types of variables are called strongly typed programming languages. Strong typing is also known as static typing, since data types do not change after they have been declared. Programming languages that do not require you to declare the data types of variables are called loosely typed programming languages. Loose typing is also known as dynamic typing, since data types can change after they have been declared. JavaScript language is a loosely typed programming language.
changingVariable = "Hello World"; // String
changingVariable = 8; // Integer
changingVariable = 5.367; // Floating-point number
changingVariable = true; // Boolean
changingVariable = null; // null
| Values returned by typeof() operator | |
|---|---|
| Return Value | Returned For |
| number | Integers and floating-point numbers |
| string | Text string |
| boolean | True or False |
| object | Objects, array, and null variables |
| function | Functions |
| undefined | Undefined variables |
Example: Tutorial3Ex1.html
An integer is a positive or negative number with no decimal place. Integer values in JavaScript can range from -9007199254740990 (-253) to 9007199254740990 (253).
A floating-point number contains decimal places or is written using exponential notation. Exponential notation, or scientific notation, is a way of writing very large numbers with many decimal places, using a shortened format. Numbers written in exponential notation are represented by a value between 1 and 10 multiplied by 10 raised to some power. The value of 10 is written with an uppercase or lowercase E. Example: 200,000,000,000 as 2.0e11. Floating point values in JavaScript range from approximately between -1.7976931348623157 x 10308 and +1.7976931348623157 x 10308. The decimal numbers of a floating point number are between -5 x 10-324 and 5 x 10-324. If the floating point value is exceed the range, its value will be infinity or -infinity.
Example: Tutorial3Ex1.html
A Boolean value is a logical value of true or false. JavaScript converts the values true and false to the integer 1 and 0 when necessary. For example, when you attempt to use a Boolean variable of true in a mathematical operation, JavaScript converts the variable to an integer value of 1.
Example: Tutorial3Ex1.html
A text string contains zero or more characters surrounded by double or single quotation marks. Empty string (var1 = "") is valid value for string literal and is not considered to be null or undefined.
Example: Tutorial3Ex1.html
| JavaScript Escape Sequences | |
|---|---|
| Escape Sequence | Character |
| \b | Backspace |
| \f | Form feed |
| \n | New line |
| \r | Carriage return |
| \t | Horizontal tab |
| \' | Single quotation mark |
| \" | Double quotation mark |
| \\ | Backslash |
| String Object | |
|---|---|
| Method | Description |
| anchor(anchor name) | Adds an <A>...</A> tag pair to a text string |
| big() | Adds a <BIG>...</BIG> tag pair to a text string |
| blink() | Adds a <BLINK>...</BLINK> tag pair to a text string |
| bold() | Adds a <B>...</B> tag pair to a text string |
| charAt(index) | Returns the character at the specified position in a text string. returns nothing if the specified position is greater than the length of the string |
| fixed() | Adds a <TT>...</TT> tag pair to a text string |
| fontcolor(color) | Adds a <FONT COLOR=color>...</FONT> tag pair to a text string |
| fontsize(size) | Adds a <FONT SIZE=size>...</FONT> tag pair to a text string |
| indexOf(text, index) | Returns the position number in a string of the first character in the text argument. If the index argument is included, then the indexOf() method starts searching at that position within the string. Return -1 if the text is not found |
| italics() | Add a <I>...</I> tag pair to a text string |
| lastIndexOf(text, index) | Returns the position number in a string of the last instance of the first character in the text argument. If the index argument is included, then the lastIndexOf() method starts searching at that position within the string. Return -1 if the character or string is not found |
| link(href) | Adds an <A HREF=URL>...</A> tag pair to a text string |
| small() | Adds a <SMALL>...</SMALL> tag pair to a text string |
| split(separator) | Divides a text string into an array of substring, based on the specified separator |
| strike() | Adds an <STRIKE>...</STRIKE> tag pair to a text string |
| sub() | Adds an <SUB>...</SUB> tag pair to a text string |
| toLowerCase() | Converts the specified text string to lowercase |
| toUpperCase() | Converts the specified text string to uppercase |
| Property | Description |
| length | Returns the number of characters in a string. Example: string_variable_name.length |
An array contains a set of data represented by a single variable name. You can think of an array as a collection of variables contained within a single variable. Each piece of data contained in an array is called an element. You create new arrays by using the new keyword and the Array() constructor function, in the same manner that you instantiate objects from your own custom constructor functions. The numbering of elements within an array starts with an index number of zero (0). The Array object length property returns the number of elements in an array.
Syntax:
var array_var = new Array(); // Declare an empty array_var
var array_var = new Array(2); // Declare array_var with two empty elements
(array_var[0] and array_var[1])
array_var[2] = "new_element"; // Add the third element into array_var
with the value of "new_element"
var array_var = new Array(5, "text"); // Declare array_var with two elements.
array_var[0] has the integer 5
// and array_var[1] has the string "text".
The following code creates a two-dimensional array and displays the results.
a = new Array(4)
for (i=0; i < 4; i++) {
a[i] = new Array(4)
for (j=0; j < 4; j++) {
a[i][j] = "[" + i + "," + j + "]"
}
}
document.write("<BR><BR>")
for (i=0; i < 4; i++) {
str = "Row " + i + ": "
for (j=0; j < 4; j++) {
str += (" " + a[i][j])
}
document.write(str, "<BR><BR>")
}
Ouputs:
Example: Tutorial3Ex1.html
| Array Object | |
|---|---|
| Method | Description |
| concat() | Combines two arrays, into a single array |
| join() | Combines all elements of an array into a string |
| pop() | Removes and returns the last element from an array |
| push() | Adds and returns a new array element |
| reverse() | Transposes elements of an array |
| shift() | Removes and returns the first element from an array |
| slice() | Creates a new array from a section of an existing array |
| splice() | Add or removes array elements |
| sort() | Sorts elements of an array |
| unshift() | Add new elements to the start of an array and returns the new array length |
| Property | Description |
| length | The number of elements in an array. Example: array_variable_name.length |
An expression is a combination of literal values, variables, operators, and other expressions that can be evaluated by the JavaScript interpreter to produce a result. You can use operands and operators to create more complex expressions. Operands are variables and literals contained in an expression. Operators are symbols used in expressions to manipulate operands.
Examples:
"this is a string variable" // string literal
expression
10
// integer literal expression
3.156
// floating-point literal expression
true
// Boolean literal expression
null
// null literal expression
employee_number //
variable expression
myNumber = 100; // this
statement is an expression that results in the value 100 being assigned to
myNumber
| Arithmetic Binary Operators | |
|---|---|
| Operator | Description |
| + | Addition. Adds two operands. 3 + 2 is 5. "3" + "2" is "32". |
| - | Subtraction. Subtract one operand from another operand |
| * | Multiplication. Multiplies one operand by another operand |
| / | Division. Divides one operand by another operand |
| % | Modulus. Divides two operands and returns the remainder |
| Arithmetic Unary Operators | |
|---|---|
| Operator | Description |
| ++ | Increment. Increases an operand by a value of one |
| -- | Decrement. Decreases an operand by a value of one |
| - | Negation. Returns the opposite value (negative or positive) of an operand |
| Assignment Operators | |
|---|---|
| Operator | Description |
| = | Assigns the value of the right operand to the left operand |
| += | Combines the value of the right operand with the value of the left operand, or adds the value of the right operand to the value of the left operand and assigns the new value to the left operand |
| -= | Subtracts the value of the right operand from the value of the left operand and assigns the new value to the left operand |
| *= | Multiplies the value of the right operand by the value of the left operand and assigns the new value to the left operand |
| /= | Divides the value of the left operand by the value of the right operand and assigns the new value to the left operand |
| %= | Modulus--divides the value of the left operand by the value of the right operand and assigns the remainder to the left operand |
| Comparison Operators | |
|---|---|
| Operator | Description |
| == | Equal. Returns true if the operands are equal |
| === | Strict equal. Returns true if the operands are equal and of the same type |
| != | Not equal. Returns true if the operands are not equal |
| !== | Strict not equal. Returns true if the operands are not equal or not of the same type |
| > | Greater than. Returns true if the left operand is greater than the right operand |
| < | Less than. Returns true if the left operand is less than the right operand |
| >= | Greater than or equal. Returns true if the left operand is greater than or equal to the right operand |
| <= | Less than or equal. Returns true if the left operand is less than or equal to the right operand |
| ? | Conditional. conditional expression ? expression1: expression2; |
| Logical Operators | |
|---|---|
| Operator | Description |
| && | And. Returns true if both the left operand and the right operand return a value of true, otherwise, it returns a value of false |
| || | Or. Returns true if either the left operand or right operand returns a value of true, otherwise, it returns a value of false |
| ! | Not. Returns true if an expression is false and returns false if an expression is true |
| Operator Precedence (left-to-right) |
|---|
| Parentheses/brackets/dot (() [] .) -- Highest precedence |
| Negation/increment (! - ++ -- typeof void) |
| Multiplication/division/modulus (* / %) |
| Addition/Subtraction (+ -) |
| Comparison (< <= > >=) |
| Equality (== !=) |
| Logical and (&&) |
| Logical or (||) |
| Assignment operators (= += -= *= /= %=) |
| Comma (,) -- Lowest precedence |