javascript Flashcards
What is the purpose of variables?
used to store information to be referenced and manipulated
How do you declare a variable?
by using the var keyward
How do you initialize (assign a value to) a variable?
by adding an equal sign to the right of the variable name
What characters are allowed in variable names?
The period, the underscore, and the characters $, #, and @
What does it mean to say that variable names are “case sensitive”?
it means that names need to be identical
What is the purpose of a string?
used for data values that are made up of ordered sequences of characters
What is the purpose of a number?
to give a number value to a variable
What is the purpose of a boolean?
to assign true or false to a variable
What does the = operator mean in JavaScript?
to assign
How do you update the value of a variable?
by reassigning it
What is the difference between null and undefined?
null is set but undefined is a return value set by javaScript
Why is it a good habit to include “labels” when you log values to the browser console?
to identify which console log belongs to which.
Give five examples of JavaScript primitives.
string, null, numbers , undefined boolean
What data type is returned by an arithmetic operation?
numbers
What is string concatenation?
adding 2 strings together
What purpose(s) does the + plus operator serve in JavaScript?
addition
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds and assign
What are objects used for?
Objects group together a set of variables and functions
What are object properties?
a simple association between name and value
Describe object literal notation.
an array of key:value pairs
How do you remove a property from an object?
by using the delete operator
What are the two ways to get or update the value of a property?
by dot notation and bracket notation
What are arrays used for?
to store more then one information at a time
Describe array literal notation.
where you define a new array using just empty brackets
How are arrays different from “plain” objects?
Objects represent properties while arrays create and store list of data in a single variable
What number represents the first index of an array?
0
What is the length property of an array?
.length
How do you calculate the last index of an array?
length - 1
What is a function in JavaScript?
a set of statements that performs a task or calculates a value
Describe the parts of a function definition.
a set of inputs, a set of outputs, and a rule that relates the elements of the set of inputs to the elements
Why are function parameters useful?
because they allow storing data that the function needs to work with
What two effects does a return statement have on the behavior of a function?
the return statement returns the values inside the function then anything after the return statement will not run.
Describe the parts of a function call.
arguments: an array-like object containing the argument passed to the currently executing function
callee: the currently executing function.
caller: the function that invoked the currently executing function.
length: the number of arguments passed to the function
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function call is invoking or calling that function a function definition is defining the function
What is the difference between a parameter and an argument?
function parameters are names listed in the function’s definition. arguments are the real values passed to the function.
Why do we log things to the console?
to see the code output
What is a method?
a method is a function which is a property of an object.
How is a method different from any other function?
method is associated with an object
How do you remove the last element from an array?
by using the pop() method
How do you round a number down to the nearest integer?
by using the method Math.floor()
How do you generate a random number?
by using the math.random method
How do you delete an element from an array?
by using the splice() method
How do you append an element to an array?
by using the push or the unshift method
How do you break a string up into an array?
by using the split method
Do string methods change the original string? How would you check if you weren’t sure?
they don’t modify the original string. the way to check it is by using console.log
Roughly how many string methods are there according to the MDN Web docs?
around 50
Is the return value of a function or method useful in every situation?
yes
Roughly how many array methods are there according to the MDN Web docs?
around 50
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
===, !== , < , >, <=, >=
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
decision-making statement that guides a program to make decisions based on specified criteria
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyward, conditoin, then the code to run if value is true
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
by using a logical operator
What is the purpose of a loop?
to repeats a sequence of instructions until a specific condition is met
What is the purpose of a condition expression in a loop?
to repeat the code until the condition is met
What does “iteration” mean in the context of loops?
Means how many times the loop will loop.
When does the condition expression of a while loop get evaluated?
Before each iteration.
When does the initialization expression of a for loop get evaluated?
Once before the loop begins.
When does the condition expression of a for loop get evaluated?
Before each loop iteration.
When does the final expression of a for loop get evaluated?
At the end of every loop iteration.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
Adds one to the operand.
How do you iterate through the keys of an object?
Using the for in statement.
Why do we log things to the console?
to see what the code is and what it does
Which “document” is being referred to in the phrase Document Object Model?
the HTML document
What is the word “object” referring to in the phrase Document Object Model?
the document is an object
Give two examples of document methods that retrieve a single element from the DOM.
document. querySelector
document. getElementByTagName()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
so we can select the value of the variable and manipulate it
What console method allows you to inspect the properties of a DOM element object?
console.dir
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
so that the document load first then lastly the script
What does document.querySelector() take as its argument and what does it return?
it takes all css selectors, tags, and it returns the selected element as a value.
What does document.querySelectorAll() take as its argument and what does it return?
it takes all css selectors, tags, and it returns All selected elements as a value.
What is a “model”?
a representation of something
What is a DOM Tree?
a collection tree that represents the dom
Why do we log things to the console?
to see the output of the code
What is the purpose of events and event handling?
used to handle and verify user input, user actions, and browser actions
Are all possible parameters required to use a JavaScript method or function?
no
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function?
a call-back function is a function passed into another function as an argument.
What object is passed into an event listener callback when the event fires?
a function
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
event.target is a reference to the object onto which the event was dispatched. console log the event.target to make sure what the value is. to learn more information about it we check the MDN documents
What is the difference between these two snippets of code?
the first one doesnt have a callback function the second one has a function as its second argument
What is the className property of element objects?
the class name property is used to manipulate the class that’s giving to an element
How do you update the CSS class attribute of an element using JavaScript?
by querySelecting the element then using the className property to give that element a different className
What is the textContent property of element objects?
the textContent property help us manipulate the text content within the element that holds the text
How do you update the text within an element using JavaScript?
by using the textContent property
Is the event parameter of an event listener callback always useful?
yes
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
because its easier to access
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a ?
the form submits
What does the event.preventDefault() method do?
it prevents the default behavior of the form
What does submitting a form without event.preventDefault() do?
it refreshes the page or sends the user to a different page
What property of a form element object contains all of the form’s controls.
elements
What property of a form control object gets and sets its value?
.value
What is one risk of writing a lot of code without checking to see if it works so far?
not console logging it
What is an advantage of having your console open when writing a JavaScript program?
to see what the code is doing
Does the document.createElement() method insert a new element into the page?
yes
How do you add an element as a child to another element?
by using the appendChild method
What do you pass as the arguments to the element.setAttribute() method?
you give it 2 arguments first is the attribute second is the value
What steps do you need to take in order to insert a new element into the page?
createElement then return that element
What is the textContent property of an element object for?
to set the text of an element
Name two ways to set the class attribute of a DOM element.
set attribute or by using className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
we are able to reuse that function and call it at anytime anywhere
What is the event.target?
a reference to the object onto which the event was dispatched
Why is it possible to listen for events on one element that actually happen its descendent elements?
because of event bubbling
What DOM element property tells you what type of element it is?
tagName
How can you remove an element from the DOM?
by using the element.remove method
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
by adding the function to the parent element and then using event bubbling
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
by adding the function to the parent element and then using event bubbling
What is the event.target?
a reference to the object onto which the event was dispatched
What is the affect of setting an element to display: none?
it hides that element display from the webpage
What does the element.matches() method take as an argument and what does it return?
it takes a css selector as a string and returns the match as a value
How can you retrieve the value of an element’s attribute?
by using getAttribute
At what steps of the solution would it be helpful to log things to the console?
at every step
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
you would have multiple eventlisteners that listen to multiple clicks
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
very long with multiple lines of code and multiple conditions that check
What is JSON?
is a text-based data format following JavaScript object syntax
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
because you can turn objects to bytes and transmit them
How do you serialize a data structure into a JSON string using JavaScript?
by using the json.stringfy
How do you deserialize a JSON string into a data structure using JavaScript?
by using json.parse
How to you store data in localStorage?
by using the set item method
How to you retrieve data from localStorage?
by using the get item method
What data type can localStorage save in the browser?
object
When does the ‘beforeunload’ event fire on the window object?
before the document loads
What is a method?
is a function which is a property of an object
How can you tell the difference between a method definition and a method call?
by the syntax of it
Describe method definition syntax (structure).
we first create an object inside the object we create a function that does something then return the function const obj = { function() { return 'hi' } };
Describe method call syntax (structure).
object.method name
How is a method different from any other function?
A method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation
What is the defining characteristic of Object-Oriented Programming?
encapsulation, inheritance and polymorphism
What are the four “principles” of Object-Oriented Programming?
Abstraction
Encapsulation
Inheritance
Polymorphism
What is “abstraction”?
The process of removing physical, spatial, or temporal details
What does API stand for?
application programming interface
What is the purpose of an API?
allows applications to access data and interact with external software components
What does the new operator do?
lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof operator do?
checks if an object is giving a type