Download presentation
Presentation is loading. Please wait.
1
Javascript: variables and parameters
20 March 2014
2
What we want to do
3
Form with input, button, output
HTML JavaScript
4
Add data HTML JavaScript
5
Push button and input data sent to javascript
HTML JavaScript
6
Javascript uses the data to create a new result
HTML JavaScript
7
And moves it to the output location
HTML JavaScript
8
How do we do it?
9
variables A place to hold a value
Mailbox: know where to pick up my mail; don’t know what’s in it How to define? var name; var name = initial-value;
10
Assignment statements
target = new-value; CHANGE the value of the target variable TO the new-value new-value can be a constant, a variable, or an expression x = 3; x = y; x = x+ 5;
11
Using values from forms
Value in the form can be treated just like a variable! Giving it a value User input Assign it a value in an onclick Define it in the HTML
12
FUNCTION: collection of instructions
HTML JAVASCRIPT (function.js) <head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit();”> </body> function doit () { alert(“Hi!”); }
13
parameters Just a special type of variable
Something that you hand to the function Q: Many users: how do you name? A: Give it its OWN names to use locally Q: How do you match up? A: By POSITION
14
FUNCTION with parameters
HTML JAVASCRIPT (function.js) <head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit(3,5);”> </body> function doit (a,b) { var c = a*b); alert(“product is ”+c); }
15
Return value return (value); Want to get information BACK to HTML
With a return, the function has a VALUE Can be used anywhere you can use a constant or variable Alert Assignment statement
16
FUNCTION with return HTML JAVASCRIPT (function.js)
<head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“alert(doit(3,5));”> </body> function doit (a,b) { var c = a*b); return(c); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.