Javascript, Sixth Edition: Working With Functions, Data Types, and Operators
Javascript, Sixth Edition: Working With Functions, Data Types, and Operators
Chapter 2
Working with Functions, Data Types,
and Operators
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;
}
• 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
function calculateVolume(length, width, height) {
var volume = length * width * height;
document.write(volume);
}
• 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()" />
var sum_of_numbers = a + b + c;
return result;
• 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
JavaScript, Sixth Edition 15
Understanding Variable Scope (cont’d.)
function duplicateVariableNames() {
document.write(color);
duplicateVariableNames();
document.write(color);
• Data type
– Specific information category a variable contains
• Primitive types
– Data types assigned a single value
document.write(stateTax);
stateTax = 40;
document.write(stateTax);
stateTax = null;
document.write(stateTax);
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
JavaScript, Sixth Edition 31
Working with Strings (cont’d.)
var modulusResult = 15 % 6;
• Prefix operator
– Placed before a variable
• Postfix operator
– Placed after a variable
• 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
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
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
JavaScript, Sixth Edition 60
Summary (cont’d.)