Title: The Web Warrior Guide to Web Design Technologies
1The Web Warrior Guide to Web Design Technologies
Chapter 14 JavaScript Part II
2Objectives
- In this chapter you will
- Study data types
- Use expressions and arithmetic, assignment,
comparison, conditional, and logical operators - Work with strings
3Data Types
- A data type is a specific category of information
that a variable contains. - A variables 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.
4Data 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.
5Data Types
6Data 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.
7Data 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
8Data 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.
9Data 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.
10Data 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.
11Data 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.
12Data 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.
13Expressions 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.
14Expressions 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.
15Expressions and Operators
16Expressions and Operators
17Expressions and Operators
18Expressions 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 .
19Expressions and Operators
20Expressions and Operators
- Comparison operators are used to compare two
operands and determine if one numeric value is
greater than another. - Examples of comparison operators , , !,
!, gt, lt, gt, lt. - 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.
21Expressions 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 !
22Expressions 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.
23Expressions and Operators
- Order of precedence from highest to lowest
- Parentheses/brackets/dot ( () .)
- Negation/increment (! - -- typeof void)
- Multiplication/division/modulus ( / )
- Addition/subtraction ( -)
- Comparison (lt lt gt gt)
- Equality ( !)
- Logical and ()
- Logical or ()
24Expressions and Operators
- Assignment operators ( - / )
- Comma (,)
25Strings
- 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, \\, \
26Strings
27Strings
- 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()
28Strings
- 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.
29Summary
- 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
loosely-typed programming languages.
30Summary
- 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.
31Summary
- 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.
32Summary
- 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.