Chapter 2 Lesson 1 PDF
Chapter 2 Lesson 1 PDF
Chapter 2
Lesson 1
• Calling a function
– JavaScript expression for calling a function:
functionName(paramValues);
– paramValues passed to a function are the arguments (actual
parameters)
• Returning a value from a function
– Syntax for a function that returns a value:
function functionName(parameters) {
statements
return value;
}
– return statement ends execution and returns a single value
Managing Events with Functions
• Using event handlers
– Most direct method of associating a function with an event
– Drawback: places JavaScript code in the HTML file
– Syntax for creating an event handler as an attribute of the HTML
element:
<elem onevent = "function()">
• Events as object properties
– Places the event handler within the JavaScript code file
– Can only specify function name, not parameter values
– Only one function can handle an event at a time
– Syntax for an event as an object property:
object.onevent = function;
Managing Events with Functions Continued
• Event listeners
– An event listener listens for an event as it propagates through a
web page, during either:
• The capture phase (event moves down the object hierarchy) or
• The bubbling phase (event moves back up the object hierarchy)
– Can attach multiple functions to the same event
– Syntax for method that attaches an event listener to an object:
object.addEventListener("event", function,
capture)
• Events and anonymous functions
– Include entire structure of anonymous function in place of function
name in an event handler or event listener
– Can pass in parameter values with this approach
Using Built-in JavaScript Functions
Figure 2-4
Understanding Variable Scope
Figure 2-6
Working with Data Types Continue 1
• Strongly typed (statically typed) programming languages require
that you declare the type of data that a variable contains and do not
allow you to alter that type
• Loosely typed (duck typed, dynamically typed) programming
languages do not require you to declare the data type and allow data
types to be change
• JavaScript is loosely typed
– Data types cannot be declared when variables are created
– JavaScript interpreter determines and assigns or reassigns the variable’s data
type based on the type of data stored
– diffTypes = "Hello World!"; // String
diffTypes = 8; // Integer number
diffTypes = 5.367; // Floating-point number
diffTypes = true; // Boolean
diffTypes = null; // Null
Working with Data Types Continue 2
• Working with numeric values
– Integer: positive or negative number without decimal places
– Floating point number: positive or negative numbers containing
decimal places
• Can be written in exponential notation (scientific notation):
2.0e6 = 2 ✖️ 106 = 2,000,000
– Use integers to calculate monetary values
• Calculations on integer values, but not on floating point numbers, are the
same in binary and decimal
• Working with Boolean values
– Boolean value: a logical value of true or false
– Most often used for controlling program flow or for data
comparisons
Working with Data Types Continue 3
• Working with strings
– Text string: zero or more characters surrounded by double or
single quotation marks
– Empty string: zero-length string value
– Can use quotation marks within strings:
document.write("Welcome to 'Fan Trick
Photography’”);
document.write('Welcome to "Fan Trick
Photography”’);
– To split a text string onto a new line without causing an error:
• Use two or more strings concatenated by the addition operator (+)
• For some browsers, end a line with the \ character to indicate the string
continues
• Create a template literal by enclosing the string in backtick characters (`)
Working with Data Types Continue 4