Transcript Document

The Web Warrior Guide to
Web Design Technologies
Chapter 14
JavaScript: Part II
Objectives
In this chapter you will:
• Study data types
• Use expressions and arithmetic, assignment,
comparison, conditional, and logical
operators
• Work with strings
Data Types
• A data type is a specific category of
information that a variable contains.
• A variable’s specific data type is very
important because the data type helps
determine how much memory the computer
will allocate for the data stored in the variable.
• The data type also governs the kinds of
operations that can be performed on a
variable.
Data Types
• Data types that can only be assigned a single
value are called primitive types.
• Primitive types: Integer, Floating-point
number, Boolean, String, Undefined, and
Null.
• JavaScript supports three reference data
types: Functions, Objects, and Arrays.
• Assigning a null value to a variable indicates
that the variable does not contain a value.
Data Types
Integer numbers
Positive or negative numbers
with no decimal places
Floating-point
numbers
Boolean
Positive or negative numbers
with decimal places
A logical value of true or false
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
Data Types
• Strongly-typed programming languages
require programmers to declare the data type
of variables.
• Loosely-typed programming languages do
not require the programmer to declare the
data type of variables.
• JavaScript is a loosely-typed programming
language.
Data Types
• The data type of a variable can be
determined using the typeof() operator.
• JavaScript supports two numeric data types:
Integers and Floating-point numbers.
• An Integer is a positive or negative number
with no decimal place.
• Integer values can range from
–9007199254740990 to 9007199254740990
Data Types
• A Floating-point numbers contain decimal
places.
• Exponential notation, or scientific notation, is
a shortened format for writing very large
numbers, or numbers with many decimal
places.
• The value of 10 is written with an uppercase
or lowercase E.
Data Types
• 2.0e11 means “two times 10 to the eleventh
power”.
• Floating-point values that exceed the largest
positive value of +- 1.7976931348623157 x
10^ 308 result in a special value of Infinity.
Floating-point values that exceed the smallest
negative value of +- 5 x 10 ^ -324 result in a
value of –Infinity.
Data Types
• A Boolean value is a logical value of true or
false.
• An Array contains a set of data represented
as a single variable name.
• Arrays are represented in JavaScript by the
Array object.
• Array() is a constructor function.
• Each piece of data in the Array is called an
element.
Data Types
• The numbering of elements in the array starts
at zero.
• A specific array element is referred to by
enclosing its index number in brackets at the
end of the array name.
• In JavaScript, the values assigned to an array
element can be different data types.
Data Types
• When an array is created with the Array()
constructor function, declaring the number of
array elements is optional.
• The size of an array can change dynamically.
• The number of elements in the array can be
determined using the length property.
• Values can be assigned to array elements
when the array is first created.
Expressions and Operators
• 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.
• Operands are variables and literals contained
in an expression.
• Operators are symbols used in expressions
to manipulate operands.
Expressions and Operators
• A binary operator requires an operand before
and after the operator.
• A unary operator requires a single operand
either before or after the operator.
• Arithmetic operators are used to perform
mathematical calculations.
• The prefix operator is placed before a
variable.
Expressions and Operators
Operator
Type
Arithmetic
Description
Assignment
Assigns values to variables
Used for performing mathematical
calculations
Comparison Compares operands and returns a
Boolean value
Logical
Used for performing Boolean operations
on Boolean operands
String
Performs operations on strings
Expressions and Operators
Operator
Type
Description
Special
Used for various purposes that includes the
conditional, instance of, in, delete, void,
new, this, typeof and comma operators.
Expressions and Operators
Operator
Description
+ (addition)
Adds two operands
- (subtraction)
Subtracts one operand from another
* (multiplication) Multiplies one operand by another
/ (division)
Divides one operand by another
% (modulus)
Divides one operand by another and
returns the remainder
Expressions and Operators
• The postfix operator is placed after a variable.
• The increment (++) and decrement (--) unary
operators can be used as prefix and postfix
operators.
• Assignment operators are used for assigning
a value to a variable.
– Examples of assignment operators: =, +=, -=, *=,
/=, and %=.
Expressions and Operators
Operator
Description
++ (increment) Increases an operand by a value of 1
-- (decrement) Decreases an operand by a value of
1
- (negation)
Returns the opposite value of an
operand
Expressions and Operators
• Comparison operators are used to compare
two operands and determine if one numeric
value is greater than another.
– Examples of comparison operators: ==, ===, !=,
!==, >, <, >=, <=.
• The conditional operator executes one of two
expressions, based on the result of a
conditional expression. A conditional
expression returns a Boolean value and
determines whether to execute a conditional
or looping statement.
Expressions and Operators
• The syntax of a conditional operator is:
conditional expression ? expression1: expression2
• Logical operators are used for comparing two
Boolean operands for equality.
– Examples of logical operators: &&, ||, and !
Expressions and Operators
• Operator precedence is the order of priority in
which operations in an expression are
evaluated. Expressions are evaluated from
left to right, with the highest priority
precedence evaluated first.
• If all the operators in the expression have the
same priority of precedence, then the
expression is evaluated from left to right.
Expressions and Operators
• Order of precedence from highest to lowest:
–
–
–
–
–
–
–
–
Parentheses/brackets/dot ( () [] .)
Negation/increment (! - ++ -- typeof void)
Multiplication/division/modulus (* / %)
Addition/subtraction (+ -)
Comparison (< <= > >=)
Equality (== !=)
Logical and (&&)
Logical or (||)
Expressions and Operators
– Assignment operators (= += -= *= /= %=)
– Comma (,)
Strings
• The escape character is a special character
that tells the interpreter that the character
following it has a special purpose.
• The escape character in JavaScript is ‘\’.
• The combination of the escape character with
other characters is called an escape
sequence.
– Examples of escape sequences: \n, \b, \f, \\, \”…
Strings
Escape Sequence Character
\b
Backspace
\f
\n
\r
Form feed
New line
Carriage return
\t
\’
\”
\\
Horizontal tab
Single quotation mark
Double quotation mark
Backslash
Strings
• The concatenation operator for strings is +.
• The += assignment operator can be used to
combine two strings.
• The JavaScript String object contains
methods for manipulating text strings.
• Commonly used methods are: big(), blink(),
bold(), charAt(index), fixed(), fontColor(color),
fontSize(size), indexOf(text, index), italics()…
Strings
• The length property determines the number
of characters in the string.
• Parsing refers to the act of extracting
characters of substrings from a larger string.
Summary
• A data type is the specific category of information that
a variable contains.
• Data types that can be assigned only a single value
are called primitive types.
• Reference, or composite, data types can contain
multiple values or complex types of information.
• Programming languages that require you to declare
the data types of variables are called strongly-typed
programming languages.
• Programming languages that do not require you to
declare the data types of variables are called looselytyped programming languages.
Summary
• An Integer is a positive or negative number with no
decimal places.
• A Floating-point number is a number that contains
decimal places or is written using exponential
notation.
• A Boolean value is a logical value of true or false.
• An Array is a variable that contains a set of data
represented by a single variable name.
• 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.
Summary
• Operands are variables and literals contained in an
expression.
• Operators are symbols used in expressions to
manipulate operands.
• Arithmetic operators are operators that are used to
perform mathematical calculations, such as addition,
subtraction, multiplication, and division, in JavaScript.
• Assignment operators are operators that are used for
assigning a value to a variable.
• Comparison operators are operators that are used to
compare two operands to determine if one numeric
value is greater than another.
Summary
• The conditional operator executes one of two expressions
based on the results of a conditional expression.
• Logical operators are used for comparing two Boolean operands
for equality.
• Operator precedence is the order of priority in which operations
in an expression are evaluated.
• An escape character is a special character that tells the
compiler or interpreter that the character following it has a
special purpose.
• Parsing refers to the act of extracting characters or substrings
from a larger string.
• The JavaScript String object contains methods for manipulating
text strings.