HOME        NEXT        PREV

Tutorial 3: Data Types and Operators


HOME        TOP

Section A: Using Data Types

Data Types

Primitive Types
Data TypeDescription
Integer numbersPositive or negative numbers with no decimal places
Floating-point numbersPositive or negative numbers with decimal places or numbers written using exponential notation. Ex: 2.5e3=2.5*10*10*10=2500
BooleanA logical value of true or false. JavaScript converts the values of true and false to the integers 1 and 0 in a mathematical operation.
StringText such as "Hello World". 
UndefinedA variable that has never had a value assigned to it, has not been declared, or does not exist
NullAn 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 ValueReturned For
numberIntegers and floating-point numbers
stringText string
booleanTrue or False
objectObjects, array, and null variables
functionFunctions
undefinedUndefined variables

    Example: Tutorial3Ex1.html


HOME        TOP

Numeric Data Types

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


HOME        TOP

Boolean Values

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


HOME        TOP

Strings

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 SequenceCharacter
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\'Single quotation mark
\"Double quotation mark
\\Backslash

String Object
MethodDescription
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
PropertyDescription
lengthReturns the number of characters in a string. Example: string_variable_name.length


HOME        TOP

Arrays

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". 

Two-dimensional array

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
MethodDescription
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
PropertyDescription
lengthThe number of elements in an array. Example: array_variable_name.length


HOME        TOP

Section B: Expressions and Operators

Expressions

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


HOME        TOP

Arithmetic Operators

Arithmetic Binary Operators
OperatorDescription
+ 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
OperatorDescription
++ 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

    Example: Tutorial3Ex2.html

HOME        TOP

Assignment Operators

Assignment Operators
OperatorDescription
=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 

    Example: Tutorial3Ex2.html

HOME        TOP

Comparison Operators

Comparison Operators
OperatorDescription
==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;

    Example: Tutorial3Ex2.html

HOME        TOP

Logical Operators

Logical Operators
OperatorDescription
&& 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

    Example: Tutorial3Ex2.html

HOME        TOP

Operator Precedence

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

    Example: Tutorial3Ex2.html

HOME        TOP

 

 

Hosted by www.Geocities.ws

1