JavaScript Flashcards
What is the purpose of variables?
to store data values that can be used later
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
variableName = value;
What characters are allowed in variable names?
letters, underscore, dollar sign, numbers (but variable name cannot start with a number)
What does it mean to say that variable names are “case sensitive”?
variable names with different casing constitute different variables
What is the purpose of a string?
to store words, letters, and characters
What is the purpose of a number?
to store numeric values
What is the purpose of a boolean?
to store the value of true/false
What does the = operator mean in JavaScript?
assigns a value to a variable
How do you update the value of a variable?
= assignment operator
What is the difference between null and undefined?
null must be intentionally assigned to a variable, while undefined is automatically assigned when a variable was not assigned a value
Why is it a good habit to include “labels” when you log values to the browser console?
to make clear which variables are being logged and in what order
Give five examples of JavaScript primitives.
number, string, boolean, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combines strings
What purpose(s) does the + plus operator serve in JavaScript?
addition for numbers and concatenation for strings
What data type is returned by comparing two values ( < , > , === , etc)?
boolean
What does the += “plus-equals” operator do?
adds the value of the operand to the variable and assigns the result to the variable
What are objects used for?
group together related variables and functions
What are object properties?
variables that are part of an object which have a unique key name and correlated value
Describe object literal notation.
object in curly braces { } each key separated from its value using a colon, each key/value pair separated by commas
How do you remove a property from an object?
delete objectName.propertyName;
What are the two ways to get or update the value of a property?
objectName.propertyName (dot notation) or objectName[“propertName”] (bracket notation)
What are arrays used for?
storing a list of values
Describe array literal notation.
square brackets [ ] each value is separated by a comma
How are arrays different from “plain” objects?
arrays are ordered and the keys are the index numbers (rather than the property names)
What number represents the first index of an array?
0
What is the length property of an array?
number of items in the array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
a block of code designed to perform a particular task
Describe the parts of a function definition.
function keyword, (optional) function name, comma-separated list of 0+ parameters surrounded by parentheses, curly braces, (optional) return statement
Describe the parts of a function call.
function name + parentheses with arguments (if function definition has them)
When comparing them side-by-side, what are the differences between a function call and a function definition?
the definition has the function keyword and the code block with curly braces
What is the difference between a parameter and an argument?
parameters are placeholders in the function definition; arguments are passed to the function when it is called
Why are function parameters useful?
way to provide data to get different results from functions
What two effects does a return statement have on the behavior of a function?
1) causes the function to produce a value; 2) exits the function
Why do we log things to the console?
to debug
What is a method?
a function that is a property of an object
How is a method different from any other function?
it is called on an object
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?
.splice( ) or .pop( ) or .shift( )
How do you append an element to an array?
.push( )
How do you break a string up into an array?
.split( )
Do string methods change the original string? How would you check if you weren’t sure?
no; can check by logging the string to the console or read up MDN
Is the return value of a function or method useful in every situation?
not necessarily
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?
conditionally runs a block of code
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, condition in parentheses, curly braces
What are the three logical operators?
&& , || , !
How do you compare two different expressions in the same condition?
&& or ||
What is the purpose of a loop?
to repeat a set of steps
What is the purpose of a condition expression in a loop?
to determine when the loop should stop
What does “iteration” mean in the context of loops?
the code block being executed once
When does the condition expression of a while loop get evaluated?
each time before the code block is executed
When does the initialization expression of a for loop get evaluated?
once at the beginning of the loop, before the condition
When does the condition expression of a for loop get evaluated?
each time before the code block is executed
When does the final expression of a for loop get evaluated?
each time after the code block is executed
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 1
How do you iterate through the keys of an object?
for ( var key in object )
What is a “model”?
a representation of something
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?
JavaScript objects which represent the different parts of the web page
What is a DOM Tree?
an object model that represents the structure of a webpage
Give two examples of document methods that retrieve a single element from the DOM.
querySelector( ) , getElementById( )
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?
saves the browser from having to look through the DOM tree to find the same elements again
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?
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?
takes a CSS selector, returns node of the first matching element
What does document.querySelectorAll( ) take as its argument and what does it return?
takes a CSS selector, returns a nodelist of all matching elements
What is the purpose of events and event handling?
to enable the web page to interact with users
What do [ ] square brackets mean in function and method syntax documentation?
optional
What is a callback function?
a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
the event object that contains all relevant info about the event
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
the element where the event originated from; can read about it on MDN
What is the className property of element objects?
enables you to get and set the value of the class attribute of the specified element
What is the textContent property of element objects?
the text that is in the containing element (and its children)
How do you update the text within an element using JavaScript?
.textContent
Is the event parameter of an event listener callback always useful?
not necessarily because you don’t always need info about the event
Why is storing information about a program in variables better than only storing it in the DOM?
better way to save and store data values, such as info collected from users
What does the transform property do?
allows you to modify the coordinate plane of the element
Give four examples of CSS transform functions.
rotate, scale, skew, translate
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
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?
the browser will automatically reload the page with the form’s values in the URL
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?
makes it difficult to debug, and you might have to rewrite all of it if there are errors
What is an advantage of having your console open when writing a JavaScript program?
check and debug code in real time
Does the document.createElement() method insert a new element into the page?
no, it creates an element node but it is not on the page yet
How do you add an element as a child to another element?
.appendChild( ) or .append( )
What do you pass as the arguments to the element.setAttribute() method?
(attributeName, attributeValue)
What steps do you need to take in order to insert a new element into the page?
createElement( ) , [ assign .textContent ] , appendChild( )
What is the textContent property of an element object for?
to get/set the text content of an element and its children
Name two ways to set the class attribute of a DOM element.
element.setAttribute(class, classValue) , or assign value as the className property
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
so you can easily repeat the code block without having to redo the work; can make the website dynamic
What is the event.target?
the element where the event originated from; a reference to the object onto which the event was dispatched
What is the effect of setting an element to display: none?
hides the element and removes it from the document flow
What does the element.matches() method take as an argument and what does it return?
takes a CSS selector string, returns a boolean of whether 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 the value of a variable changes
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 to add an event listener for each tab
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 have to manually set the class name for each tab and view
What is a method?
a function that is a property of an object
How can you tell the difference between a method definition and a method call?
the method definition has a corresponding object property key, the function keyword, and curly braces which contain the function code block
Describe method definition syntax (structure).
an object is defined with a property key and the corresponding function, which has the function keyword, parameters separated by commas in parentheses, and curly braces which contain the function code block
Describe method call syntax (structure).
objectName.methodName(arg1, arg2,…)
How is a method different from any other function?
it must be called on an object, can access data stored on the object
What is the defining characteristic of Object-Oriented Programming?
pairs data with behavior
What does API stand for?
Application Programming Interface
What is “abstraction”?
simplifying a complex process so that it is easy to use
What is the purpose of an API?
simplifies programming through abstraction
What is this in JavaScript?
it is an implicit parameter
What does it mean to say that this is an “implicit parameter”?
the variable value 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); } };
the object named character
Given the above character object, what is the result of the following code snippet? Why?
character.greet( );
“It’s-a-me, Mario!” - calling the greet method of the character property, which contains the firstName property of this object (the character object)
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello( );
“It’s-a-me, undefined!” - we do not know what the value of this will be since there is no object to the left of the dot
How can you tell what the value of this will be for a particular function or method definition?
the object in which the method is defined
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot; if there is no value to the left of the dot when the function is called, this will be the global window object (by default)
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
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?
methods are defined on a “prototype” object and are borrowed when they’re needed
If an object does not have its own property or method by a given key, where does JavaScript look for it?
the prototype object
What does the new operator do?
1) creates a blank object 2) adds a __proto__ property to the new object that links to the constructor function’s prototype object 3) binds the newly created object instance as the this context 4) returns this if the function doesn’t return an object
What property of JavaScript functions can store shared behavior for instances created with new?
__proto__
What does the instanceof operator do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object (returns a boolean)
What is a “callback” function?
a function passed into another function as an argument
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?
a positive integer value which identifies the timer created (ID)
What is AJAX?
a technique for loading data into part of a page without having to refresh the entire page
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
Bonus Question: An XMLHttpRequest object has an addEventListener( ) method just like DOM elements. How is it possible that they both share this functionality?
both have the EventTarget prototype within their prototype chains
What is the purpose of a conditional?
to make decisions in code
What is a code block? What are some examples of a code block?
the code within curly braces, such as the code block of functions, conditionals, and loops
What does block scope mean?
the value of the variable is available only within the code block
What is the scope of a variable declared with const or let?
within the code block
What is the difference between let and const?
variables declared with let can be reassigned while variables declared with const cannot
Why is it possible to .push( ) a new value into a const variable that points to an Array?
because we are not reassigning a new value, just modifying the contents of the original value
What is the syntax for writing a template literal?
text is wrapped in backticks ( ` )
What is “string interpolation”?
substituting part of a string for the values of variables or expressions
What is destructuring, conceptually?
a way to assign the parts of an object/array to individual variables
What is the syntax for Object destructuring?
const/let { propertyName1: alias1, propertyName2: alias2 } = objectName;
What is the syntax for Array destructuring?
const/let [ var1, var2, var3 ] = arrayName;
How can you tell the difference between destructuring and creating Object/Array literals?
the object/array literal is on the left-hand side of the = for destructuring, and it is on the right-hand side for creating
What is the syntax for defining an arrow function?
parentheses around the parameters, followed by => , followed by curly braces containing the code block
When an arrow function’s body is left without curly braces, what changes in its functionality?
must be an expression - do not need a return statement
How is the value of this determined within an arrow function?
the arrow function captures the this value of the enclosing context instead of creating its own this context