How to embed Javascript in HTML - Answers <script> tag
<script src="filename.js"></script>
How Javascript is executed - Answers Script file executed from top to bottom and run by the browser
for-loops - Answers for (let i = 0; i < 5; i++) { ... }
while-loops - Answers while (notFinished) { ... }
Conditionals - Answers If statements
if (...) {
...
} else {
...
}
Writing a function - Answers function name( ) {
statement;
statement;
...
}
Calling a function - Answers name( );
Variables in Javascript - Answers var, let, const
Always use const or let
var - Answers Function scope variable, same as let but dated
let - Answers Block scope variable
const - Answers Block scope constant, can't be reassigned
Function scope variable - Answers Only go out of scope at the end of functions so you can refer to the
same variable after the block has ended (after the loop or if-statement in which they are declared)
Block scope variable - Answers Can only be referred to within block (in the same brackets)
, Value types of variables - Answers Boolean, number, string, symbol, null, undefined, *object
Boolean type variables - Answers True or False, use && (and) || (or) ! (negation)
Example.
let isTeenager = age > 12 && age < 20;
age = 15;
isTeenager = true
String type variables - Answers Anything in single or double quotes
Null type variables - Answers Value meaning "this has no value"
Undefined type variables - Answers The value of a variable with no value assigned
Arrays - Answers Object types used to create lists of data, 0-based indexing, can check size via length
property
Example.
let list = [ ]; //creates empty list
let groceries = ['milk', 'cocoa puffs'];
groceries [1] = 'kix'; //changes cocoa puffs to kix
let first5 = [1,2,3,4,5]
console.log(first5[3]); //prints 4
Event-driven programming - Answers Code doesn't run right away but executes after some event fires
(click, hover, key, etc.)
Event Handler - Answers Any function listening to an event that executes when it occurs
Event Listener - Answers Tells function to run when event occurs
addEventListener(event name, function name);