Javascript Flashcards
DOM
the browsers rendering of your HTML file (NOT the html file by itself)… we can manipulate it with javascript. when you refresh, all changes to DOM go away.
Variables
what you use to store data in memory when programming
the entity used to tell our program to remember a value for later.
stored in memory.
made up of two parts: declaration and assignment. think of it like a bucket in memory that is filled with data (declaration = bucket ; assignment = value/data stored in bucket)
Conditionals
used to make decisions using comparisons. value can be TRUE or FALSE.
if(age > 18){ //value is true so do this stuff } else{ //value is not true so do this stuff } Comparisons: - equality - 9===9 is true 7===3 false "Hello" ==== "Hello" true
Loops
Functions. How to create? How to call?
set of instructions
//declaration: function name(parameters) { //body }
//calling the function: name(arguments)
Steps involved with variable creation
declaration: let age
assignment: age = 25
both at same time: let age = 25
declaration is like creating a bucket for data
assignment is like putting something in the bucket.
what is a variable declaration? what does it do?
it creates a space in memory
example: let age
let is what allows it to create a new space in memory. it triggers the PC to be like “SOMETHING IS TRYING TO MAKE A DATA BUCKET”
what is variable assignment?
assigning a value to a declared space of memory.
types of data
numbers, strings, etc
What is a String
stores text; always surrounded by double quotes(“), single quotes(‘), or ticks(`)
may need to use an escape character depending on order…. using \
What are some data types within that are Numbers? How can you manipulate them?
int - whole numbers
float - decimal points
note: can be signed (positive or negative)
manipulation: + - * / % (modulus - the remainder)
what can modulus be used for to figure out?
figuring out if a number is even or odd (x%2==0 means even). figuring out if a number is divisible by another number.
fizzbuzz programming challenge:
if divisible by 3 print fizz, divisible by 5 print buzz, divisible by 3 and 5 print fizzbuzz
what happens to user interactions involving javascript when the page is refreshed?
everything resets.
difference between = == === != !==
= is assignment
== is comparing if values are same
=== is comparing if values and data types are the same
!= checks if values are NOT the same
!== checks if values or data types are NOT the same
how do you add Leon’s smurf to an element, waiting for a click to happen?
document.querySelector(“#elementID”).addEventListener(‘click’, myFunction)
function myFunction() { //do what you want with the click here }
change an element’s(id=”dog”) text content to “chihuahua”
document.querySelector(“#dog”).innerText = “chihuahua”
what are constants? what are best practices for naming them?
constants are variables that can only be assigned once.
if you know the value by run time, like const COLOR_ORANGE = "#FF7F00";
then its recommended to put name in all caps.
if they are assigned during run time, just do it normally. like const pageLoadTime = /* */
argument vs parameter
parameter = variable listed inside patentheses in function declaration (declaration time term)
atgument = value thats passed to function when it is called (call time term)
we declared functions listing their parameters, then call them passing arugments
example:
function showMessage(from, text) {
alert( from + ‘: ‘ + text );
}
showMessage(from, “Hello”);
In the example above, one might say: “the function showMessage is declared with two parameters, then called with two arguments: from and “Hello””.
how do you set a default parameter for a function?
function myFunction(message = "default message"){ }
or (message = anotherFunction()) - which will store the value that function returns by default