Javascript Flashcards
What is JavaScript?
High level language that humans understand, that can be broken down into basic computer language (binary code)
What is the DOM
Document object model = everything in inspector is NOT the HTML / CSS. It is the RENDERING of these files.
You can manipulate the DOM (rendering of files) via JavaScript.
What happens to the DOM when you refresh a webpage?
the DOM (document object model) resets completely and starts from scratch
How does JavaScript affect our HTML / CSS codes.
JavaScript manipulates and changes the way a DOM is rendered, not the files. JS works on the browser itself.
What’s an event listener?
Sensors in your DOM that respond to specific stimulus and activate a specific program/code action in response.
4 key aspects to know in JavaScript
Variables, conditionals, functions, loops
What are variables
What we can use to tell program to store/remember a value for use later on.
What is a declaration
What we use to create a space in memory
Ex. “ let age “
What’s an assignment
Assignment = action of setting a unit/info to a declaration/memory bucket.
Ex . Age = 25
What 2 steps of a variable? Give example
- Create a space in memory (declaration)
- Assign a value (assignment)
Let age = 25
What are variable conventions? Ex. Camel case
The way to write variables.
Camel case = first word low caps
Next words = upper case first letters
What two common types of data can you store in your variables?
Numbers
Strings (pieces of text)
What are strings? How to write them?
Piece of text, surrounded by double quotes, single quotes, or ticks (all preference)
What you use on outside CANT be used on inside:
Example : ‘They “purchased” it’
If you want to use double quotes, how to escape that?
Put back slash \ before each quote:
“They \”purchased\” it”
What 2 kinds of numbers in JavaScript?
Integers (isn’t) : 29 (whole numbers)
Float: 5.19398 (number with decimal places)
What signs for multiply? Division? Modulus?
Multiply *
Divide /
Modulus %
What is 10 mod 4?
- (4 goes into 10 twice with 2 left over)
Mod = modulus = remainders.
What are conditionals in JavaScript?
If - condition = activate program
What does “ x = = 3 “ mean? How is it different from x = = = 3
Two = means equal in VALUE
Three = means equal in Value and TYPE
What does ! Mean in “ x ! = 8
! = not
X doesn’t not equal 8
What is conditional syntax?
If ( condition is true ) {
/ / do cool stuff
}
How to program multiple conditionals : if the first doesn’t work..
If ( condition is true ) {
Do this cool stuff
} else if (condition is true ) {
Do this other cool stuff )
} else {
Do default stuff
}
Const vs let
Const = Constance (never changing variable_)
Let = variable that can change or be re-assigned
How to write a double conditional? (2 conditions at one time)
If ( condition 1 && condition 2 ) {
Action
}
Must use 2 ampersands
How to write a double (either or) condition
If ( condition 1 || condition 2 ) {
Action
}
What is pseudo code and why is it important?
Being able to talk out what you want your code to do, and then coding it.
Allows you to code in any language.
How to declare and assign multiple variables at once?
Let user = ‘ John ‘
, age = 25
, message = ‘ yellow ‘ ;
Let message = ‘ hi ‘ ;
Let message = ‘ hey ‘ ;
Why would this error?
Only declare a variable once; don’t use let twice.
Const hi = ‘ 3.14 ‘ ;
Const hi = ‘4.56’ ;
Why would this error?
A Const can NEVER be re-assigned. It never changes.
When would we use upper case in our Constants names?
On variables that are super hard to remember but are know BEFORE execution.
Ex. Hexadecimal numbers.
When naming your variables, what are the two rules?
- Only letters, numbers and the TWO symbols ( $ and _ )
- First character of name CANT be a number
What are the 2 types of constants?
Constants that are know PRIOR TO EXECUTION (ex. A birthday)
Constants that are CALCULATED IN RUN_TIME (ex. Loading time)
What’s a function?
Function = A set of codes to perform a task ( ex. Eat = go to kitchen, prepare food, chew, swallow…)
What is the syntax of a function?
Function + name of function ( parameter1, parameter 2… ) {
Function body
}
When thinking about functions, what are global vs local variables? What is the best practice for using each?
Global = variables created outside function
Local = variables created inside of function.
- try to minimize use of global variables. Creating mostly local variables inside functions = better organization.
When you have two variables with the same declaration (one local and one outer variable), which variable will the function use?
The local (inside) variable.
What are parameters in functions?
Information you want the function to use, when it is running it’s code.
Ex. Function adder ( num1, num2 ) {
Console . Log ( num1 + num 2 ) ;
}
Adder ( 5 , 10 )
- function will now add 5 + 10
When using functions, what are parameters vs arguments?
Parameters = the tools a function will use to do its job.
Arguments = the materials you give the function to use with its parameters.
How to set the default parameter when you don’t provide an argument?
Function functionName ( parameter = “ default setting “ ) {
What is the basic structure of naming a function. Give example.
Prefix verb + “what”
Ex. calcSum
What are common prefix names for functions?
Get = return a value
Calc = calculate
Create
Check
Is it good or bad to make your function do more than one action?
Bad. One function = one action.
What is “self-describing” code?
You keep functions really simple and accurately named. No functions with more than one action.
Makes better organization & easy to read/understand later
Better to use local or global variables for functions?
Local = better = better organization & cleaner code
What is x ! = 8
X is not equal to 8
X = = 8 means…
X equals 8 (in terms of value)
What is x = = = “3” mean
X is exactly equal to (value and type)
What is x ! = = “3” mean
X is not equal (neither value nor type)
X! = = 3 vs x ! = = “ 3 “
Quotes = a string
No Quotes = an integer
X > 8 means…
X is greater than 8
What does x < 8 mean?
X is less than 8
X > = 8 means…
X is greater than or equal to 8
X < = 8 means….
X = is less than or equal to 8
What are logical operators?
Relationships in conditionals. Ex. X = = = “3”
When writing conditionals, how many else if’s can you have? What happens to the code if you have multiple else if’s?
You can have infinite else if’s. If multiple, the code will move in sequential order.
What does the = mean in JavaScript?
= means your assigning something in a variable
Explain the “danger of assignment vs comparison”
Be careful using = in your comparisons/conditionals. Only ONE = will mean assignment.
You want to create a code that takes values from different inputs and combines them together into a string and inserts it into another place.
Document . Queryselector ( ‘ #ID of insert location ‘ ) . InnerText = ` $ { Input ID 1 } $ { input ID 2} `
What’s pseudo code? How do you do it? Why is it important?
Pseudo code = being able to talk ABOUT your code.
How = talk out what you want to do and WRITE it.
Important because will allow you to program in any language.
How to write a comment in JS?
Start with //
How to write a class in JS
‘Classname’
What does this mean:
Variable. Classlist. Toggle ( ‘hidden ‘ )
Toggle = change from on to off ( or opposite)
‘ hidden ‘ = a class
In Java script, what are you telling the computer to do with this code?
“ document. Queryselector ( ‘ # check ‘ ) . AddEventListener ( ‘ click ‘ , check )
“ hey. Go to the DOM, and look (queryselector) for a code with the ID, “Check”, put an event listener on it. When it senses a click, activate the “check” function.
what is a function expression. give an example
When you create a variable and assign it a FUNCTION.
let sayHi = function ( ) {…
how are function expressions different from normal functions
Because function expressions are assigned to variables, they don’t have their own name. Its just function:
let sayHI = function ( )
why do function expressions have a ; at the end?
because the ; is part of a normal variable assignment syntax.
ex. let name = function; just like
let name = 2;
what are callback functions?
putting functions inside other functions
dogage++ vs ++ dogage
dogage ++ = variable total plus 1
++ dogage = 1 plus doggage