JavaScript Flashcards
What is the purpose of variables?
Used to store data
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
var variableName = “string”
What characters are allowed in variable names?
letters, numbers, $ , and _.
What does it mean to say that variable names are “case sensitive”?
S != s. Uppercase and lowercase letters will be interpreted differently.
What is the purpose of a string?
Consists of letters and other characters. They are frequently used to add new content to a page and they can contain HTML markup
What is the purpose of a number?
Numeric data types handle numbers.
What is the purpose of a boolean?
Have one of two values: true or false. Booleans are helpful when determining which part of a script should run.
What does the = operator mean in JavaScript?
= assigs a value to a variable
How do you update the value of a variable?
assign a new value to the variable
Why is it a good habit to include “labels” when you log values to the browser console?
To avoid confusion of having multiple console.logs
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combining multiple strings together
What purpose(s) does the + plus operator serve in JavaScript?
concatenation and addition
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Adds by a specified value and sets the variable to the value of the expression
What are objects used for?
storing key-value pairs. Used to model real-life objects
What are object properties?
storage areas connected to an object that can store data
Describe object literal notation.
{key: value}
How do you remove a property from an object?
delete operator
What are the two ways to get or update the value of a property?
dot notation and bracket notation
What are arrays used for?
Storing data. Particularly useful for lists.
Describe array literal notation.
[ value, value,… ]
How are arrays different from “plain” objects?
They are numerically indexed
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?
array.lenght - 1
What is a function in JavaScript?
collection instructions that can be repeated whn called
Describe the parts of a function definition.
function keyword, name, parameter, , the start of the code block, return statement, end of the code block
Describe the parts of a function call.
functionName(arguments)
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call takes arguments in parentheses. a function definition has parameters and code block
What is the difference between a parameter and an argument?
a parameter is a place holder (variable). arguments are the values passes to a parameter
Why are function parameters useful?
allows you to pass information to a function
What two effects does a return statement have on the behavior of a function?
returns a value to function and stops execution of code
Why do we log things to the console?
for debugging
What is a method?
Actions that are performed on objects
How is a method different from any other function?
a method exists as a property on an object. a function can be defined anywhere
How do you remove the last element from an array?
.pop( ) method
How do you round a number to the nearest integer?
.round( ) method
How do you generate a random number?
.random( ) method of the Math object
How do you delete an element from an array?
.splice( ) method
How do you append an element to an array?
.push( ) method
How do you break a string up into an array?
.split( ) method
Do string methods change the original string? How would you check if you weren’t sure?
No. check with console.log.
Roughly how many string methods are there according to the MDN Web docs?
40 - 50
Is the return value of a function or method useful in every situation?
No. Some functions just modify and change things. Others are used to return something
Roughly how many array methods are there according to the MDN Web docs?
A lot
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?
Evaluates a condition to determine if code will be run
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (condition) {
code block
}
What are the three logical operators?
&& || !
How do you compare two different expressions in the same condition?
With a logical operator
What is the purpose of a loop?
to run a block of code until a certain condition is met
What is the purpose of a condition expression in a loop?
determines whether or not the code is run depending on boolean value
What does “iteration” mean in the context of loops?
each pass through a loop
When does the condition expression of a while loop get evaluated?
at the beginning of each iteration
When does the initialization expression of a for loop get evaluated?
before any code has been run
When does the condition expression of a for loop get evaluated?
at the beginning of each iteration
When does the final expression of a for loop get evaluated?
at the end of each loop
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?
The increment operator (++) increments (adds one to) its operand and returns a value.
How do you iterate through the keys of an object?
for in loop
Why do we log things to the console?
For debugging and checking functionality
What is a “model”?
A representation of an object
Which “document” is being referred to in the phrase Document Object Model?
An HTML Page
What is the word “object” referring to in the phrase Document Object Model?
Each object represents a different part of the page loaded in the browser window
What is a DOM Tree?
Representation of the DOM that is made up of nodes (document node, element nodes, attribute nodes, and text nodes).
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.
querySelectorAll( )
Why might you want to assign the return value of a DOM query to a variable?
It is helpful when you want to work with the element more than once
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?
The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
A CSS selector is taken as an argument and the first matching element is returned
What does document.querySelectorAll() take as its argument and what does it return?
A CSS selector is taken as an argument and all matching elements are returned in a node list
Why do we log things to the console?
For debugging and checking functionality
What is the purpose of events and event handling?
To allow the script to respond when certain events and interactions occur with the page
What do [] square brackets mean in function and method syntax documentation?
It means it is optional
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 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?
the event object
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. Check MDN documentation to get more information about it.
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The function with parentheses would indicate that it should run when the page loads. The function without parentheses will run when the event is fired.
What is the className property of element objects?
The className property gets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
.className property
Element.className = cName
What is the textContent property of element objects?
represents the text content of the node and its descendants.
How do you update the text within an element using JavaScript?
.textContent property
Element.textContent = ‘ ‘
Is the event parameter of an event listener callback always useful?
Generally useful
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?
You can access it quicker. Do not rely on the DOM.
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?
preventDefault() method 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?
the page would reload.
What property of a form element object contains all of the form’s controls.
form.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?
It will be difficult to find where your code stopped working
What is an advantage of having your console open when writing a JavaScript program?
gives you the ability to check code
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?
Turns off the display of an element and acts as if it was not there.
What does the element.matches() method take as an argument and what does it return?
a selector is used as an arguments. Returns a boolean
How can you retrieve the value of an element’s attribute?
getAttribute( )
At what steps of the solution would it be helpful to log things to the console?
Everywhere
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 an event listener for each tab and view
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?
You would need conditionals for each
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What is the event.target?
a reference to the object onto which the event was dispatched
What DOM element property tells you what type of element it is?
tagName
What does the element.closest() method take as its argument and what does it return?
A selector is used as an argument. It returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. If there isn’t such an ancestor, it returns null.
How can you remove an element from the DOM?
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?
using the parent node as the eventlistener
What is JSON?
JSON is a text-based data format following JavaScript object syntax. JSON exists as a string — useful when you want to transmit data across a network.
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
By allowing an Object to be converted into stream of bytes so that it can be transferred over a network or stored in a persistent storage. Vis versa, the string can be pulled from a network and converted to an object.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse
How to you store data in localStorage?
.setItem( )
How to you retrieve data from localStorage?
.getItem( )
When does the ‘beforeunload’ event fire on the window object?
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.
What is a method?
A method is a function which is a property of an object. There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or Static Methods which are tasks that are called directly on an object constructor.
How can you tell the difference between a method definition and a method call?
Method call-
object.method()
method definition- method: function() { // }
Describe method definition syntax (structure).
method: function() { // }
Describe method call syntax (structure).
object.method()
How is a method different from any other function?
A method is associated with an object while a function is not
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
Being able to work with (possibly) complex things in simple ways.
What does API stand for?
Application programming interface
What is the purpose of an API?
the purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
What data type can local storage save in the browser
String
What is this in JavaScript?
a keyword which contains a value which is defined at call time
What does it mean to say that this is an “implicit parameter”?
it is available in a function’s code block even though it was never included in the function’s parameter list or declared with var.
When is the value of this determined in a function; call time or definition time?
call time
What does this refer to in the following code snippet?
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
Mario
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
“It’s-a-me Mario!”
The greet method of the character object is call. This method includes a string that is concatenated with this.FirstName.
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet; hello();
undefined.
this is attached to hello when it is called with hello. hello does not have a firstName property. this is attached to whoever is calling the function.
How can you tell what the value of this will be for a particular function or method definition?
it will refer to the object it is in
How can you tell what the value of this is for a particular function or method call?
the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).
What kind of inheritance does the JavaScript programming language use?
prototype based
What is a prototype in JavaScript?
An object that contains properties and (predominantly) methods that can be used by other objects.
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
The methods are defined on a “prototype” object and arrays simply borrow those methods when they’re needed.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
prototype chain
What does the new operator do?
- Creates a blank, plain JavaScript object;
- Links (sets the constructor of) the newly created object to another object by setting the other object as its parent prototype;
- Passes the newly created object from Step 1 as the this context;
- Returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
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.
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setTimeout( )
How can you set up a function to be called repeatedly without using a loop?
setInterval( )
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout();
The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval()
What is Array.prototype.filter useful for?
Creating a new array from an existing array that contains the values that pass a given callback function
What is Array.prototype.map useful for?
creating a new array containing the values resulting from calling a provided callback function on each value of an array
What is Array.prototype.reduce useful for?
for reducing an array to a single value using a provided callback function
What does fetch() return?
a promise
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
specify the request method as the value of the method property in the init object
What must the return value of myFunction be if the following expression is possible?
myFunction()();
a function
What does this code do? const wrap = value => () => value;
a function that takes a single parameter and returns a function that returns the value of that parameter
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
when it is defined
What allows JavaScript functions to “remember” values from their surroundings?
closures