Chapter 2 - Functions - Data Types - Operators
Chapter 2 - Functions - Data Types - Operators
Sixth Edition
Chapter 2
Working with Functions, Data Types, and
Operators
2
Objectives
Methods
Procedures associated with an object
Functions
Related group of JavaScript statements
Executed as a single unit
Virtually identical to methods
Not associated with an object
Must be contained within a script element
Named function
Related statements assigned a name
Call, or reference, named function to execute it
Anonymous function
Related statements with no name assigned
Work only where they are located in code
Use named function when you want to reuse code
Use anonymous function for code that runs only once
Function definition
Lines making up a function
Named function syntax
function name_of_function(parameters) {
statements;
}
Anonymous function syntax
function (parameters) {
statements;
}
JavaScript, Sixth Edition
6
Defining Functions (cont'd.)
Parameter
Variable used within a function
Placed within parentheses following a function name
Multiple parameters allowed
calculateVolume(length, width, height)
Function statements
Do the actual work
Contained within function braces
Put functions in an external .js file
Reference at bottom of body section
Passing arguments
Sending arguments to parameters of a called function
Argument value assigned to the corresponding parameter value in the
function definition
Handling events
Three options
Specify function as value for HTML attribute
<input type="submit" onclick="showMessage()" />
function averageNumbers(a, b, c) {
var sum_of_numbers = a + b + c;
return result;
}
JavaScript, Sixth Edition
15
Understanding Variable Scope
Variable scope
Where in code a declared variable can be used
Global variable
Declared outside a function
Available to all parts of code
Local variable
Declared inside a function
Only available within the function in which it is declared
Cease to exist when the function ends
Keyword var required
function duplicateVariableNames() {
document.write(color);
duplicateVariableNames();
document.write(color);
JavaScript, Sixth Edition
Data type
Specific information category a variable contains
Primitive types
Data types assigned a single value
var stateTax;
document.write(stateTax);
stateTax = 40;
document.write(stateTax);
stateTax = null;
document.write(stateTax);
diffTypes = null;
JavaScript, Sixth Edition
// Null
25
Understanding Numeric Data Types
5 "</p>");
Text string
Contains zero or more characters
Surrounded by double or single quotation marks
Can be used as literal values or assigned to a variable
Empty string
Zero-length string value
Valid for literal strings
Not considered to be null or undefined
(excerpt)</h1>");
document.write(speaker);
document.write(date);
String operators
Concatenation operator (+): combines two strings
var destination = "Honolulu";
Plus sign
Concatenation operator and addition operator
Binary operator
Requires an operand before and after the operator
Unary operator
Requires a single operand either before or after the operator
var divisionResult = 15 / 6;
var modulusResult = 15 % 6;
Prefix operator
Placed before a variable
Postfix operator
Placed after a variable
+= compound addition
assignment operator
Used to combine two strings and
to add numbers
Examples:
Examples: (cont’d.)
Comparison operators
Compare two operands
Determine if one numeric value is greater than another
Boolean value of true or false returned after compare
Operands of comparison operators
Two numeric values: compared numerically
Two nonnumeric values: compared in alphabetical order
Number and a string: convert string value to a number
If conversion fails: value of false returned
Conditional operator
Executes one of two expressions based on conditional expression
results
Syntax
conditional expression ? expression1 : expression2;
var result;
document.write(result);
Operator precedence
Order in which operations in an expression evaluate
Associativity
Order in which operators of equal precedence execute
Left to right associativity
Right to left associativity
Evaluating associativity
Example: multiplication and division operators
Associativity of left to right
var x = 3;
var y = 2;
x = y *= ++x;
Functions
Similar to methods associated with an object
Pass parameters
To execute, must be called
Variable scope
Where a declared variable can be used
Global and local variables
Data type
Specific category of information a variable contains
Static typing and dynamic typing