Javascript Flashcards
What is the purpose of variables?
To store data to use a later time
How do you declare a variable?
Use the keyword Var
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
Letters, Numbers, Underscores and $
What does it mean to say that variable names are “case sensitive”?
Variables are specific to how they are typed
What is the purpose of a string?
To store and manipulate text
What is the purpose of a number?
To store a value
What is the purpose of a boolean?
To determine if something is false or true
What does the = operator mean in JavaScript?
Assigning a value to a variable
How do you update the value of a variable?
Change the value
What is the difference between null and undefined?
Null is an empty value and Undefined means that variable has been declared but not defined
Why is it a good habit to include “labels” when you log values to the browser console?
Point of reference
Give five examples of JavaScript primitives.
String Numbers Null Undefined Boolean
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Combination of 2 or more string values
What purpose(s) does the + plus operator serve in JavaScript?
To add values or concatenate strings
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
Adds the value of the right operand to the variable and the assigns the result to the variable
What are objects used for?
To group variables or functions
What are object properties?
Variables stored in an obeject
Describe object literal notation.
{properties: 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 Bracket Notation
What are arrays used for?
To be put in a list
Describe array literal notation.
[values, values, values]
How are arrays different from “plain” objects?
Arrays are numeric and will repair themselves if something is deleted
What number represents the first index of an array?
0
What is the length property of an array?
Array.length
How do you calculate the last index of an array?
Array.length - 1
What is a function in JavaScript?
Series of statements that is repeatable
Describe the parts of a function definition.
Function keyword name of function (parameter list) code block return statement function
Describe the parts of a function call.
Name of the function and arguments if it requires it
When comparing them side-by-side, what are the differences between a function call and a function definition?
call has arguments and passes a value and definition has code block with steps
What is the difference between a parameter and an argument?
Parameters are the names listed in the function’s definition Arguments are the real values passed to the function
Why are function parameters useful?
its a tool that is generalized
What two effects does a return statement have on the behavior of a function?
Return statement ends the execution of a function, and returns control to the calling function
Why do we log things to the console?
To debug
What is a method?
Function being stored in a property
How is a method different from any other function?
Methods have to say where they’re coming from
How do you remove the last element from an array?
object.pop()
How do you round a number down to the nearest integer?
object.floor()
How do you generate a random number?
object.random() function getRandomNumberInRange(start,end) { var randomNumber = math.floor(math.random()*(end-start) + 1) + start return randomNumber }
How do you delete an element from an array?
object.splice()
How do you append an element to an array?
object.push() or use object.unshift() to prepend
How do you break a string up into an array?
object.split()
Do string methods change the original string? How would you check if you weren’t sure?
No because strings are immutable. Use console log to debug
Roughly how many string methods are there according to the MDN Web docs?
Alot
Roughly how many array methods are there according to the MDN Web docs?
30
Roughly how many array methods are there according to the MDN Web docs?
Alot
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?
Making decisions
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (condition) {}
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
&& and ||
What is the purpose of a condition expression in a loop?
To tell the loop when to stop
What is the purpose of a loop?
To allow us to repeat code
What does “iteration” mean in the context of loops?
In the beginning
When does the condition expression of a while loop get evaluated?
The condition is checked before each iteration
When does the initialization expression of a for loop get evaluated?
Is before anything
When does the condition expression of a for loop get evaluated?
Is evaluated once before every iteration
When does the final expression of a for loop get evaluated?
After each 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?
Increments the value by 1
How do you iterate through the keys of an object?
For in
Why do we log things to the console?
To debug
What is a “model”?
Representation of the original
Which “document” is being referred to in the phrase Document Object Model?
The HTML doc
What is the word “object” referring to in the phrase Document Object Model?
Javascript Objects
What is a DOM Tree?
Represents the page so that programs can change the document structure, style, and content
Give two examples of document methods that retrieve a single element from the DOM.
.getElementByID()
.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?
To be able to access it better
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?
So all the contents can load first
What does document.querySelector() take as its argument and what does it return?
Returns the first Element within the document that matches the specified selector, or group of selectors
What does document.querySelectorAll() take as its argument and what does it return?
Takes in node list and return everything
Why do we log things to the console?
To debug
What is the purpose of events and event handling?
To verify user inputs
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 function being passed as a value
What object is passed into an event listener callback when the event fires?
Information about the event that has occurred
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It is a reference to the object to where the event occurred. More information could be found on MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
First one has a variable
Second one has a function being called
What is the className property of element objects?
Used to update the class attribute and get the class
How do you update the CSS class attribute of an element using JavaScript?
Query select the the class and use the .classname to update the attribute
What is the textContent property of element objects?
Allow is to up date text
How do you update the text within an element using JavaScript?
.textcontent property on element using the DOM
Is the event parameter of an event listener callback always useful?
No
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?
We don’t want to look elsewhere for the information
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?
Prevents default action
What does submitting a form without event.preventDefault() do?
Deletes the data
What property of a form element object contains all of the form’s controls.
Elements property
What property of form a control object gets and sets its value?
Value property
What is one risk of writing a lot of code without checking to see if it works so far?
The code can potentially be broken and will be more difficult to find out where the code went wrong
What is an advantage of having your console open when writing a JavaScript program?
You’ll be able to see an errors as they occur when writing your code
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?
append.child()
What do you pass as the arguments to the element.setAttribute() method?
‘name of attribute’, ‘value of the attribute’
What steps do you need to take in order to insert a new element into the page?
create function
assign var to document.createElement(‘name of element’) method and appendChild to that var and return it from the function and call it
What is the textContent property of an element object for?
Contains the text within the element
Name two ways to set the class attribute of a DOM element.
setAttribute, className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
We dont have to physically write in anything to the document reuse the function on a different section
What is the event.target?
Stores event where it originated from
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?
.tagName
What does the element.closest() method take as its argument and what does it return?
Selector returns the dom tree
How can you remove an element from the DOM?
object.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 event listener to that parent
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?
Removed from the document flow
What does the element.matches() method take as an argument and what does it return?
Selector string and returns a boolean (if element matches css selector)
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?
All the time
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?
Add event listener for the new tab node
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?
Write code for every situation
What is a method?
A function stored in a property of an object
How can you tell the difference between a method definition and a method call?
Definition: “property:function() include code block”
Call :”property.method()”
Describe method definition syntax (structure).
Definition: “property:function()”
Describe method call syntax (structure).
Call: “property.method()”
How is a method different from any other function?
Method are functions attached to objects
What is the defining characteristic of Object-Oriented Programming?
Objects can contain data and behavior
What is “abstraction”?
Taking something complex and simplifying it
What does API stand for?
Application programming interface
What is the purpose of an API?
Delivers a user response to a system and sends the system’s response back to a user.
What is the purpose of an API?
Delivers a user response to a system and sends the system’s response back to a user.
What is “this” in JavaScript?
Where the object code is run
What does it mean to say that “this” is an “implicit parameter”?
It is include in the function scope even though its not defined
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); } };
Nothing
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
Its me mario because calling this property
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
Its me undefined because the object doesn’t exist
How can you tell what the value of “this” will be for a particular function or method definition?
You cant
How can you tell what the value of “this” is for a particular function or method call?
The object of the method is being invoked on it. this is only a value when the method is being invoked it is the left of the dot
What kind of inheritance does the JavaScript programming language use?
Prototype based inheritance
What is a prototype in JavaScript?
JavaScript objects inherit features from one another
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?
Prototypal inheritance
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
proto
What does the new operator do?
Creates a blank, plain JavaScript object.
Adds a property to the new object “(__proto__) “ that links to the constructor function’s prototype object
Binds the newly created object instance 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?
Returns true or false if object on the left matches the right
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 default time
What do setTimeout() and setInterval() return?
intervalID