JavaScript Flashcards
What is the purpose of variables?
To temporarily store bits of information a script needs to do its job
How do you declare a variable?
set a variable name to a variable keyword like var
How do you initialize (assign a value to) a variable?
set a value to the variable name with an assignment operator = < > …
What characters are allowed in variable names?
letters, dollar sign or underscore, it cannot start with a number
What does it mean to say that variable names are “case sensitive”?
capitalization matters
What is the purpose of a string?
strings are data types that consist of letters and other characters, enclosed in quotes, frequently used to add new content to a page
What is the purpose of a number?
for numeric data and counting, calculating sums, measurements, time, etc.
What is the purpose of a boolean?
to indicate whether something is true or false
What does the = operator mean in JavaScript?
the equals operator assigns a value to a variable
How do you update the value of a variable?
use the variable name and equals sign to assign it a new value
What is the difference between null and undefined?
without setting a variable name to a variable keyword, the name will be null, without assigning a value to a variable, it will be undefined.
null is a nonexistent or invalid object
Why is it a good habit to include “labels” when you log values to the browser console?
to help you debug code, so you know which variable is being logged
Give five examples of JavaScript primitives.
null, string, number, boolean, undefined, symbol, bigint
What data type is returned by an arithmetic operation?
an expression?
What is string concatenation?
combining two strings into one
What purpose(s) does the + plus operator serve in JavaScript?
concatenation, addition, forcing a variable into a number, incrementing by one for a double plus
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
addition assignment, total += 1 would mean total = total + 1
What are objects used for?
storing multiple properties related to a variable
What are object properties?
variables that tell us about the object
Describe object literal notation.
an object’s properties and methods are assigned a variable name with the equals operator
How do you remove a property from an object?
delete object name . property name
What are the two ways to get or update the value of a property?
dot notation or square bracket with property in quotes
What are arrays used for?
a list of values that are related to each other
Describe array literal notation.
name a variable and assign it values within brackets, separated by commas
How are arrays different from “plain” objects?
because with arrays, the order matters. values are accessed by their numerical order
What number represents the first index of an array?
0
What is the length property of an array?
the length property calculated the total number of indexes in an array
How do you calculate the last index of an array?
.length - 1
What is a function in JavaScript?
Functions are a series of instructions to be carried out when called.
Describe the parts of a function definition.
functions begin with the function keyword, an optional name, (parameters, more, more, etc…), the function code block beginning with {, an optional return statement, and a closing } for the code block
Describe the parts of a function call.
a function call is initiated by the function name, parenthesis, and parameters.
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function definition does not include arguments, only parameters
What is the difference between a parameter and an argument?
parameters are placeholders for future arguments
Why are function parameters useful?
They allow you to use the function more than once
What two effects does a return statement have on the behavior of a function?
- It produces a value we can use.
2. It prevents any more code from being run.
Why do we log things to the console?
to debug, to ensure our code is working properly, to test a function before pairing it with another element
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?
A method is specifically tied to or contained within a function, some are built in, and we can add our own methods to objects.
How do you remove the last element from an array?
.pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array?
with splice(), or shift()
How do you append an element to an array?
with push() - which puts it at the end of the array, or unshift() which puts it at the beginning of the array
How do you break a string up into an array?
with split() and an argument dictating where to split the array
Do string methods change the original string? How would you check if you weren’t sure?
no, you could console.log the original array after calling a method
Roughly how many string methods are there according to the MDN Web docs?
around 60
Is the return value of a function or method useful in every situation?
no, like pop
Roughly how many array methods are there according to the MDN Web docs?
40
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?
to check if the following code should continue running, to filter out edge cases, etc.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword precedes the condition ( ) inside the condition are operands (values being compared) and an operator/s like < > ===, followed by opening curly brace for code block that will execute if condition is met ( or true )
What are the three logical operators?
and && or || not !
How do you compare two different expressions in the same condition?
by using and && or or ||
What is the purpose of a loop?
loops check a condition, if its true, the code block will run and then it’s checked again
What is the purpose of a condition expression in a loop?
to determine how many iterations a code will run
What does “iteration” mean in the context of loops?
how many times the code block runs
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?
the first time the code is run
When does the condition expression of a for loop get evaluated?
immediately after the initialization expression and again until the condition is met
When does the final expression of a for loop get evaluated?
after the loop and until the condition is met
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?
increment the counter by 1
How do you iterate through the keys of an object?
for in loop
Why is using !important considered bad practice?
It makes debugging more difficult by breaking the natural cascading in your stylesheets
Why do we log things to the console?
To check and debug code before continuing a function or passing values to other functions
What is a model
A facsimile, something that is representative of another thing, a prototype, an ideal, a platonic form
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 pages elements are objects or nodes that you can access and navigate
What is a DOM Tree?
The dom tree is a js element for an html element with it’s properties, elements, children
Give two examples of document methods that retrieve a single element from the DOM.
getElementById and querySelector
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName, getElementsByTagName, querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
if you want to use the same element more than once. For instance, if you want to alter its innerHTML and setAttribute and addEventListener
What console method allows you to inspect the properties of a DOM element object?
console.dir
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
1.If you have code in your JavaScript that alters HTML as soon as the JavaScript file loads, there won’t actually be any HTML elements available for it to affect yet, so it will seem as though the JavaScript code isn’t working, and you may get errors. 2.If you have a lot of JavaScript, it can visibly slow the loading of your page because it loads all of the JavaScript before it loads any of the HTML. When you place your JavaScript links at the bottom of your HTML body, it gives the HTML time to load before any of the JavaScript loads, which can prevent errors, and speed up website response time.
What does document.querySelector() take as its argument and what does it return?
a css selector, and it returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
a css selector, and it returns all that match
Why do we log things to the console?
to debug and see what we’re doing
Are all possible parameters required to use a JavaScript method or function?
No? Just the object
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is the purpose of events and event handling?
Events are fired to notify code of “interesting changes” that may affect code execution. These can arise from user interactions such as using a mouse or resizing a window, changes in the state of the underlying environment (e.g. low battery or media events from the operating system), and other causes.
What is a callback function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
What object is passed into an event listener callback when the event fires?
an event type
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The target property of the Event interface is a reference to the object onto which the event was dispatched. The element of where the event occurred.
What is the difference between these two snippets of code?
the second one is pointless because there are no arguments, both will fire the handleClick, but the first is the better option because the handleClick function may take arguments?
What is the className property of element objects?
The className property of the Element interface gets and sets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
with the className property
What is the textContent property of element objects?
The textContent property of the Node interface represents the text content of the node and its descendants.
How do you update the text within an element using JavaScript?
.textContent =
Is the event parameter of an event listener callback always useful?
no, you can use an anonymous function right there and avoid it
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
more difficult
Why is storing information about a program in variables better than only storing it in the DOM?
by storing them in variables you can reuse them
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 ?
submit
What does the event.preventDefault() method do?
stop the form info from being sent
What does submitting a form without event.preventDefault() do?
the page refreshes
What property of a form element object contains all of the form’s controls.
elements
What property of form a 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?
you don’t know where it’s wrong
What is an advantage of having your console open when writing a JavaScript program?
you can use debugger and see what’s happening
Does the document.createElement() method insert a new element into the page?
No, not untill it’s appended
How do you add an element as a child to another element?
appendChild
What do you pass as the arguments to the element.setAttribute() method?
name, value
What steps do you need to take in order to insert a new element into the page?
createElement, createTextNode(optional), appendChild
create new element
store new element
query for parent element
call appendchild on parent with the new element as an argument
What is the textContent property of an element object for?
The textContent property of the Node interface represents the text content of the node and its descendants.
Name two ways to set the class attribute of a DOM element.
setAttribute with name, className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can give that code a name, it’s easier to test, easier to reuse elsewhere
What is the event.target?
the place where the event occurred
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What DOM element property tells you what type of element it is?
element.tagname
What does the element.closest() method take as its argument and what does it return?
The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
How can you remove an element from the DOM?
The Element.remove() method removes the element from the tree it belongs to.
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 it to the parent
What is the event.target?
where the event occurred
What does the element.matches() method take as an argument and what does it return?
The matches() method checks to see if the Element would be selected by the provided selectorString – in other words – checks if the element “is” the selector.
How can you retrieve the value of an element’s attribute?
element.getAttribute
At what steps of the solution would it be helpful to log things to the console?
after you query an element, and again after you create any new variable