JavaScript Flashcards
What is a variable?
A way to hold data
Why are variables useful?
It can hold data that changes
What two special characters can a variable begin with?
dollar sign and underscore
How do you declare a variable?
Using the keyword “var”
How do you assign a value to a variable?
With the assignment operator “=”
Are variables case sensitive?
Yes
Which words cannot be used as variable names?
can’t start with a digit, no punctuation or special characters, keywords
What is a string?
datatype to represent text stored with quotation marks
What is the string concatenation operator?
+
What is the difference when it comes to using single quotes or double quotes ( ‘ ‘ or “ “ )?
No difference, but has to start and end with same quote
How do you escape quotation characters?
Backslash before the quote to let the interpreter know that it is part of the string instead the end of it.
What is type coercion?
JS will convert that value to the type it needs
What is a number in JavaScript?
Numeric Values
What is an arithmetic operator?
Operators can do math
Name four of the arithmetic operators?
+ (addition), - (subtraction), * (multiplication), / (division) %(remainder, modulus)
What is the order of execution?
By precedence - mult, div, comes first. Parenthesis overpowers everything.
What is a boolean?
data type that is true or false
What is a comparison operator?
> < and it returns a boolean
What is the difference between undefined and null?
undefined is an absence of a value, null is intentional absence of object value
What is a function?
group a series of statements together to perform a specific task
Why are functions useful?
reusable code
How do you call a function?
function name ( )
What are the parts of a function definition?
function keyword, function name, parameter, code block
What is the difference between a parameter and an argument?
declaring a function they are parameters and calling a function they are arguments
What is the difference between null and undefined?
Null is an object and undefined is undefined
Why do you always use === for comparisons?
Due to type coercion
Do all if statements require an else statement?
No
What is the proper syntax for using the or operator?
|
What is the primary use case for switches?
Comparing a value to many values
Does the default case have to be at the bottom of the switch statement?
No it can jump back to the default
What happens if there is no break statement between cases?
It will continually run all cases even if it’s met
What is an object in JavaScript?
Set of variables and functions / collection of related data
How do you create an object literal?
curly braces, and its contents
What is a property in relation to JavaScript objects?
variables
How do you remove a property from an object?
delete keyword object name and property
When should you use bracket notation over dot notation with objects?
variable that you need to substitute
What is an array in JavaScript?
list or set of values that are related to each other that are ordered.
How do you create an array literal?
[ ]
What are the keys for the values inside an array?
index number
Arrays have a property named length. Because arrays have a properties, what other data structure are they similar to?
objects
What is the primary advantage to storing your selected elements in a variable?
To be able to use it without selecting the elements again
Why might you need JavaScript to modify the DOM after the page loads?
better user experience and manipulate data without refreshing the page.
How can you better prepare when writing HTML for DOM manipulation in your JavaScript code?
class name, ID
What are the differences between innertext and textContent?
innerText gets only the text and not the elements that are hidden
What datatype are the values you remove from a text input?
String
Why is it so important to be able to dynamically update text?
A way to communicate with the user
What are some of the drawbacks of HTML Event Handler Attributes?
To keep HTML and Javascript separate
Why is the Window.event property to be avoided in new code?
Old technique
What is the difference between the getElementById() method and the querySelector() method?
QuerySelector chooses the first one that matches the argument
Who passes in the event object into the handleClick callback function?
JavaScript
Does a callback function require a name?
You can have anonymous but should always have a name
What is the purpose of a loop?
Reiterate through elements
Why might there be different kinds of loops?
While loops are better to have a value you are comparing. For loops are useful when you have a variable you want to increment
What is the purpose of a conditional expression as it relates to loops?
To let the loop know when to end or continue the loop
Could a loop potentially go on forever?
Yes if the expression is never found false.
Could a loop never start?
Yes. if the expression never results true
What potential use cases are there for for loops?
iterate through an array.
Which pieces of information provided in the parentheses for a for loop are mandatory?
nothing
What is a for in loop?
Iterates to property names in objects
How do you target the value of a property in an object.
bracket notation within that for in loop
When should you use a for in loop?
To debug
What is the difference between the parentNode and parentElement properties?
Selects just the note and the other selects the whole element
Why is it important to be able to traverse the DOM?
Be able to target elements directly
What are two ways to target elements on the DOM?
querySelector and getElementBy
What is another way to add a text node to an element other than using textContent.
createTextNode and appendChild
How do you create a HTML element using vanilla Javascript?
createElement()
Why is using loops and arrays for creating multiple dom elements preferable to creating them one at a time?
More efficient, instead of writing the same code every single time
Why are arrays preferred over objects for the functionality referenced in question 1?
Array is index and easy to loop over and keep one collection of data
Why is it important to be able to retrieve and use data from inputs?
To be able to store data
Why is it dangerous to assume you have the correct data when creating elements?
Because due to user error
What is jQuery?
File included in webpages that lets one find elements using css-style selectors
What is the jQuery selector function?
jquery() or $()
What does the jQuery selector function return?
Object
Why would someone use jQuery over vanilla Javascript?
selecting elements is simpler because it uses CSS style syntax
cross browser
made a selection and can apply multiple methods to it
What are some downsides from using jQuery over vanilla Javascript?
library is very large and has to parse
Why do we use the tag at the bottom of the body, instead of putting it inside the <head> tag?
the way the document parses is from top to bottom
Why do we use the tag at the bottom of the body, instead of putting it inside the <head> tag?
the way the document parses is from top to bottom
How do you get the text out of an HTML element?
.text method
How do you get the value out of an HTML input field?
.val method
What’s the difference between .text() and .html()?
.text only returns the content from every element in the selection as opposed to .html returns the html inside the first element.
What does .on() do?
handle events
What does .on() do?
handle events
What is event bubbling?
starts the event that triggered the event and goes up to each parent.
Target and current target
Target - the element that triggered the event
currentTarget - what element was actually attached to the event handler
Target and current target
Target - the element that triggered the event
currentTarget - what element was actually attached to the event handler
What is the first argument the function setTimeout takes?
Function
If the second argument is not passed into the setTimeout function, what is the default value?
0 so immediately
What are some scenarios where setTimeout can be useful?
Modal to pop up,
What argument does the clearInterval function take?
ID that is returned by the set interval function
What does the term blocking mean?
Delaying the code to execute because the call stack is blocked
What is the call stack?
data structure - pushes and pops to the stack. arrangement of actions that need to be taken
What is the call stack?
data structure - pushes and pops to the stack. arrangement of actions that need to be taken
What is a modal?
overlay a small element over a website
What are some use cases for modals?
Overlays to advise for activity, information, approval
Why does a modal usually need to occupy the entire height and width of the viewport?
to not let the user evade it
What is this?
This is used to return values in the properties of the object
What does bind do?
method that sets the this keyword to the object passed in
What is the difference between a function and an object literal?
functions have 3 methods - apply, bind, call but are also objects still.
What is the first thing that happens in a class when it is instantiated with the new keyword?
Always first makes an empty object
Since classes are technically functions as well. Can you name a difference between classes and functions?
Use the new keyword
What is a static method?
attached to the creator