Console
Two key uses Error Messages Print Stmts If you see an error message Don’t panic Use it to help Like validation Tries its best Error often earlier Remember: JavaScript executes one statement at a time in order Track work Can leave it forever
Variables
Variable Values Save into a variable and the value changes Use a variable (like printing) and the value does NOT change
Illustration
JavaScript Function
What is a function? Collection of statements that can be invoked as a unit Because sometimes we want to do more that a single assignment!
Different Types of Functions Who writes? JavaScript provides Someone else writes & you use You write
Does function need information? Alert does…tell it what to write Asking for a random number doesn’t Always need to use () Pass data if needed () if not
Parameters Math.round: Number to round Call a function with a different value every time MUST give it everything that it expects alert or console.log: string to print Math.round: Number to round
Statement or part of an expression Alert is a statement; doesn’t give you anything back If you ask for a random number, you expect to get something back
Functions as Statements Performs an action but doesn’t give you anything back Example: alert
Alert String to print Function
Writing a Function
Format of a Function functionName(parm,parm); Always needs () even if no parameter functionName(); For now: no parameter
JavaScript Functions Summary
alert console.log Takes an input value (string) Does not return a value
Getting a random number Math.random() Does not need any inputs Returns a value between 0 and 1
Other math functions Take an input value (number) Math.round(num) Math.floor(num) Math.ceil(num) Take an input value (number) Return a value (number)
Connecting JavaScript to HTML
Where Do JavaScript Functions Go? Like CSS, can be inline or a separate file Like CSS, an external file is considered better practice Like CSS, link to it from the head section
A Separate JavaScript File <script src="myscripts.js"></script> Myscript.js JavaScript option in komodo will work on it next
FUNCTION: connecting to HTML <head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit();”> </body> HTML file name function name function doit () { alert(“Hi!”); } JAVASCRIPT (function.js)