JavaScript Flashcards
- What is a function in JavaScript?
a set of statements that performs a task or calculates a value
- Describe the parts of a functiondefinition.
Function keyword
the first line (including the argument declarations), and the so-called body of the function
- Describe the parts of a functioncall.
the name of the function being executed followed by a list of values, called arguments, which are assigned to the parameters in the function definition
- When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
The definition has a code block while the call doesn’t
Parameter are a new thing while the call is just inserting something
Function keyword
- What is the difference between aparameterand anargument?
Parameter are place holder names for when something is defined
Arguments are used when it is called .
- How do you compare two different expressions in the same condition?
Using one of the logical operators &&, ||
What is the event.target?
It is a reference to the object onto which the event was dispatched
Its the element that the event originated
Why is it possible to listen for events on one element that actually happen its descendent elements?
Bubbling
What DOM element property tells you what type of element it is?
Element tag name; property comes back upperCased
What does the element.closest() method take as its argument and what does it return?
It takes a valid css selector to match the element and it returns the closest ancestor element or itself; closest searches outwards while query is inward.
How can you remove an element from the DOM?
Using the remove method; element.remove()
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?
Add an event listener to its ancestor; event delegation
What is the affect of setting an element to display: none?
It hides its content, does not take any space, outside of the flow.
What does the element.matches() method take as an argument and what does it return?
It takes a valid css selector and returns true if the element matches the selectors otherwise false
How can you retrieve the value of an element’s attribute?
getAttribute; the value of the attribute we’re trying to get
At what steps of the solution would it be helpful to log things to the console?
After every line of code
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 need a separate function for each view
Does the document.createElement() method insert a new element into the page?
no
How do you add an element as a child to another element?
appendChild; parent.appendChild(child)
What do you pass as the arguments to the element.setAttribute() method?
Its new attribute and the new value
What steps do you need to take in order to insert a new element into the page?
First is to create a node(element) and then where you want to place it .
What is the textContent property of an element object for?
To change or add text or view current text.
Name two ways to set the class attribute of a DOM element.
setAttribute and className
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
You can reuse it
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 <form>?
Submit; it fires on the form event not the submit button
What does the event.preventDefault() method do?
tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be
What does submitting a form without event.preventDefault() do?
Your page reloads
What property of a form element object contains all of the form’s controls.
Form.elements; the elements property
What property of a form control object gets and sets its value?
The value property
What is one risk of writing a lot of code without checking to see if it works so far?
Chaos; you won’t know what code will work and what won’t.
What is an advantage of having your console open when writing a JavaScript program?
To see if there’s any errors
Why is storing information about a program in variables better than only storing it in the DOM?
You’re able to call it back at any time.
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
complicated , we wouldn’t be able to keep track. Ease of access.
How do you update the text within an element using JavaScript?
Use the textContent property to change the text of an element
What is the textContent property of element objects?
sets or returns the text content of the specified node, and all its descendants.
How do you update the CSS class attribute of an element using JavaScript?
Using className
What is the className property of element objects?
gets and sets the value of the class attribute of the specified element.; not a css selector
DOM EVENTS
Why do we log things to the console?
To verify data
What is the purpose of events and event handling?
This is a block of code (usually a JavaScript function that you as a programmer create) that runs when the event fires.register different event handlers on elements in an HTML document