Reading field values Setting field values Reading and setting paragraph text Manipulating images and text Swapping images Swapping images and setting classes Setting styles Target all elements by tag name Target some elements by tag name The DOM The DOM: Parents and children The DOM: Finding children The DOM: More ways to target elements Load more similar PDF files.
PDF Drive investigated dozens of problems and listed the biggest global issues facing the world today. Reload to refresh your session. Pdfdrive:hope Give books away. Get books you want. Ask yourself: How does my work reflect my passions, skills, and interests? Use the largest, most colorful screen available.
As for reading on a larger screen— the book includes more than 2, lines of example code. Small screens break long lines of code into awkward, arbitrary segments, jumbling the formatting.
While still decipherable, the code becomes harder to read. But even I, Vern Vertical, put my tablet into the horizontal mode to proof this book. So please: starting with Chapter 1, do yourself a favor and rotate your tablet, reader, or phone to give yourself a long line of text. Do the coding exercises on a physical keyboard. Very, very few Web developers would attempt to do their work on a phone. The same applies to the variable that's returned from a function and the variable in the calling code that catches it.
They can share the same name, but don't have to. Anywhere you can use a variable, you can use a function. Technically, a function is a variable.
You can use a function to specify the message in an alert. You can use a function in an expression. You can use a function in a function call. The first argument is merchTot.
The second argument is the function calcTax, which is also passed merchTot. Within a function, you can call another function. It passes price as an argument and receives the shipping charge back. It adds the shipping charge to the price, and returns the sum to the original calling code as a total.
You've learned that you can pass any number of arguments to any number of parameters. Unfortunately, you don't have this flexibility in the return statement. No matter how many parameters it takes or how much processing it does, a function can return only a single value to the code that calls it. That is, the difference between global and local variables. Some variables have global scope, which makes them global variables. Other variables have local scope, which makes them local variables.
Nothing could be simpler, but for some reason, when the subject comes up in books and tutorials, obfuscation often rules. This will be easy. A global variable is one that's declared in the main body of your code—that is, not inside a function. A local variable is one that's declared inside a function.
It can be either a parameter of the function, which is declared implicitly by being named as a parameter, or a variable declared explicitly in the function with the var keyword. What makes a global variable global is that it is meaningful in every section of your code, whether that code is in the main body or in any of the functions.
Global scope is like global fame. Wherever you go in the world, they've heard of Bill Clinton. A local variable is one that's meaningful only within the function that declares it. Local scope is like local fame. The mayor of Duluth is known within Duluth, but pretty much unknown everywhere else.
So there are two differences between global and local variables—where they're declared, and where they're known and can be used. Before I show you the first example, I want you to set aside what you know about passing values to a function through arguments, and passing a value back to the calling code by way of the return statement. Pretend you don't know anything about these things.
I'll come back to them later. Here's an example. The function addNumbers is called to assign it a value. Having been declared in the main code, the variable has global scope, so this function or any other function can use it. So can anything in the main code. Since the variable has global scope, the assignment is meaningful in all sections of your code, both the main code and in all functions.
The variable now has the value 4 in the function addNumbers, in the main code, and in any other functions that use it. It doesn't matter where I write But if I declare the variable not in the main code, but inside the function Everywhere else, it's unknown.
Everywhere else, it has no value at all. Since the variable theSum is declared with the keyword var inside the function, not in the main code, its scope is local.
It is meaningful only inside the function. In other functions and in the main code, it is unknown. If I write But if I write the same alert statement anywhere else—in the main code or in another function—the code breaks, because theSum is unknown outside the function.
Note: I say that a variable has local scope when you declare it in a function. By "declaring it in a function" I mean that you declare the variable explicitly with the keyword var—as opposed to casually introducing it into the function without var. The exception is if you name it as a parameter, in which case it's declared implicitly as a local variable of the function.
If you get sloppy and begin using a new variable in the body of a function without explicitly declaring it in the function with the keyword var, it is global—even though you haven't declared it anywhere in the main code. Now, to illustrate a point, I'm going to do something you'd actually never want to do in your code.
I'm going to declare a variable both in the main code and in the function. One theSum is global. The other theSum is local. This is not something you would ever want to do—it sets you up for coding mistakes and makes your code almost impossible to follow—but I did it to show the difference between global and local scope.
By declaring theSum once in the main code, and again in the function, I've created 1 a global variable that's useable almost everywhere and 2 a local variable of the same name that's useable only inside the function. Why do I say the global variable is useable almost everywhere? Because it's no good inside the function.
Inside the function, the name theSum stands for a local variable, so the name can't refer to the global variable. In this situation, coders say the global variable is in the shadow of the local variable. Inside the function, it can't be seen. Inside the function, only the local variable of that name can be seen. The local variable theSum has a value of 4 inside the function, but theSum outside the function has a value of Now let's journey a little farther into Wonderland, Alice.
I use the example only to demonstrate principles. In this code, you still have two different variables—a global variable and a local variable—that share the same name, theSum, but now, thanks to the return statement, the value of the local variable is assigned to the global variable.
Now both variables have the same name and the same value, but they're still different variables. Which brings us to a question you may be asking: If a function can use a global variable, why do you have to pass a value from an argument to a parameter? Why not just declare a global variable, then have the function use it?
Well, you can, but asking functions to work with global variables is asking them to eavesdrop on the main code, and like human eavesdropping, it invites mischief in the form of confusion and unintended consequences.
There is no controversy among coders about this. It's always best to pass values explicitly to functions through arguments. Global variables have no place in functions. You can change the value of a global variable within a function. When you do, the value of it changes everywhere, including in the main code. No return is needed. But it's better practice to use a local variable within the function, then pass that value back explicitly through a return statement.
If it's a weekday, the "Shoot me now" alert displays. It works, but it's unwieldy and a little ugly, especially if you have many conditions to test. It's time for you to learn a more elegant alternative that you can use for testing myriad conditions, the switch statement. The more conditions you need to test, the more you'll like the switch statement.
Begins with the keyword switch. Bumping up against it is the variable that's being tested, inside parentheses. Then there's an opening curly bracket. The first possibility, that the variable dayOfWeek has the value "Sat". Begins with the keyword case. Then the value that is being tried, "Sat".
Then a space and a colon. The statement that executes if the test passes—if dayOfWeek does, in fact, have the value "Sat". This statement is indented. Any number of statements can execute if the test passes. The first line of an if statement is followed by a statement or statements that executes if the condition is true. A switch statement works similarly. On the line below each case clause, there's a statement or statements that executes if the case is true.
It executes all the statements for all the cases below it. So after a true case is found and the conditional code executes, you need to jump out of the switch block by coding a break statement. If, for example, you omit the break statements in the code above, this happens: 1. An alert displays saying "Whoopee! The code that follows it executes if none of the conditions above it are met. So, in the example above, if dayOfWk isn't "Sat" or "Sun" or "Fri"—if it's anything other than those three values—an alert displays saying "Shoot me now.