Functions

CS133JS Beginning Programming: JavaScript

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Variable scope, Operators and Expressions7. Objects
3. Conditional Statements8. Methods
4. Loops9. DOM
5. Midterm10. Final

 

Table of Contents

Introduction

Q and A

Review

This review is selective. These are the concepts we need for todays new topics.

 

JavaScript Functions

Linking to an External JS File

We often put all our functions in a separate file. The file that holds the functions should have .js as the file extension.

To use the .js file in your HTML file, add this the <head> element:

We will use this for the JavaScript code in the examples below. (We won't use it to write output in an HTML element... yet).

What a Function is

Definition of a function in math

In math, a function has: input, operation(s), output f(x) = 2x Example: Coffee grinder

Functions we’ve already seen

Why Use Functions?

Writing and Calling our Own Functions

We can use pre-written functions, or write our own. We call this defining a function.

A simple function

This function has no inputs and put's its output in an alert.

  1. This is the function definition.

  1. This is the function call.

  2. Try this in the console.

  3. Now put the code into our files:

    • Put the function definition in FunctionPractice.js.

    • Put the function call inside a script element in an HTML file.

A function with one parameter

  1. Define a function with a single parameter and put it in FunctionPractice.js.

  2. Call the function in FunctionPractice.js from within a script element.
    Note that when we call a function, we call the thing we put between the parenthesis an argument (not a parameter).

     

A function with a parameter and a return value

Example:

  1. Put the function definition into practice.js

  2. Call it from the console: We see the value returned from this function in the console. If we try the same thing with the previous function, do we see a value that was returned to the console? Explain.

  3. Call it from a script element, use document.write to show the result

  4. Call it from a script element using inner.HTML function to show the result:

 

A function can have more than one parameter

  1. A function definition:

  2. The function call:

Practice: a function with all the combinations

Let's write a function is for a math quiz. It will do the following:

Think about how you would write this function, and then let's write it together.

We can test this function in the console, and then in a web page.

Summary: Parts of a function

 

Writing Output into HTML Elements

Last week we learned to write html into a web page using document.write. Now we will learn to put our JavaScript output into existing HTML elements.

Using getElementById and innerHTML

The getElementById function finds the HTML elemet with the id that is passed to the function (in the paraethesis) and returns a reference to that element. In this example, the referenced to the paragraph element is stored in the variable p. Think of the reference as an address for the element--a way to access the element later.

The innerHTML property represents the text that goes between the tags. In this case, when we assign a value to p.InnerHTML it will go between the <p> and </p> tags.

Combining statements

We can get the element and assign a value to it's innerHTML all in one statement:

 

Notes

Vocabulary

References

W3Schools

What is the HTML DOM?

HTML DOM innerHTML Property

Functions

 


 

Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, written in 2018, revised in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

 


1 Primitive data types: number, string, boolean, undefined
2 It is more accurate to say "the data type of the value contained by the variable" since varaibles themselves don't have data types.
3 We haven't learned about boolean operators yet, but we'll use one anyway: == is a boolean comparison operator. We can use it to compare two values, if they are equal the result will be true, otherwise the result will be false. For example: var isCorrect = sum == answer assuming sum is the actual sum of two numbers and answer is the answer given by the user.