JavaScript Flashcards
What is the purpose of variables?
Let you store data in your program for later use or for modification
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
Equals sign
What characters are allowed in variable names?
First character: Letter, _, $
Other characters: Letter, _, $, Number
What does it mean to say that variable names are “case sensitive”?
Capital letters are different from lowercase letters
What is the purpose of a string?
A literal representation of characters (holds letters)
What is the purpose of a number?
Contains numerical data
What is the purpose of a boolean?
True/false logic
What does the = operator mean in JavaScript?
Assigns a value to a variable
How do you update the value of a variable?
Equals sign without var
What is the difference between null and undefined?
Undefined: the variable is declared but doesn’t have a value/hasn’t been assigned
Null indicates the variable has an empty or non-existent value, must be assigned
Why is it a good habit to include “labels” when you log values to the browser console?
Makes it easier and clearer to keep track of your variables
Give five examples of JavaScript primitives.
String, Number, Boolean, Null, Undefined
What is string concatenation?
Combining strings into a new string
What purpose(s) does the + plus operator serve in JavaScript?
Add numbers together or concatenate strings
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
Adds the value on the right side to the variable and assigns the result to the variable
What are objects used for?
Storing multiple values in a single collection
Consolidating like data and modeling real world objects
What are object properties?
Essentially variables attached to an object
Describe object literal notation.
Object name {property name: 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 or bracket notation
What are arrays used for?
Storing a list of values with a particular order
Describe array literal notation.
var newArrayName = [];
How are arrays different from “plain” objects?
Arrays are organized into numeric lists
What number represents the first index of an array?
0
What is the length property of an array?
The number of values the array contains
How do you calculate the last index of an array?
The length of the array minus one
What is a function in JavaScript?
A code block that does a particular task and can be reused
Describe the parts of a function definition.
function functionName (parameter(s)names){ code block }
Describe the parts of a function call.
functionName(parater(s)youWantToPass)
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition is writing the code for the function
Function call is telling the function and all of it’s to run in that instance
What is the difference between a parameter and an argument?
Parameter is the thing that gets passed within the function definition.
Argument is the thing that gets passed when the function is called.
Why are function parameters useful?
Allow us to give data to functions for functions to perform operations on them
What two effects does a return statement have on the behavior of a function?
Return statement determines the value the function returns
It ends the execution of a function
Why do we log things to the console?
Helps to verify that our code is working as intended and to help debug
For development only
What is a method?
A function on an object
How is a method different from any other function?
A function is a set of instructions that perform a task and a method is a set of instructions that are associated with an object
How do you remove the last element from an array?
Pop()
How do you round a number down to the nearest integer?
Floor method of the math object
Math.floor()
How do you generate a random number?
Random method of the math object
How do you delete an element from an array?
Splice
How do you append an element to an array?
Push method
How do you break a string up into an array?
Use the split method and pass in an empty space (‘ ‘)
Do string methods change the original string? How would you check if you weren’t sure?
No. String are immutable
Console log the strings.
Roughly how many string methods are there according to the MDN Web docs?
Around 50
Is the return value of a function or method useful in every situation?
Not always
Roughly how many array methods are there according to the MDN Web docs?
Around 30-40. (38)
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN (the OFFICIAL source of truth)
Give 6 examples of comparison operators.
> ,
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Make decisions, compare values, make branching logic
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
If (condition to check{
Code block
}
What are the three logical operators?
! (not), ||(or), &&(and)
How do you compare two different expressions in the same condition?
Using a logical operator
What is the purpose of a loop?
Lets you repeat code over and over
What is the purpose of a condition expression in a loop?
Check to see if the loop should run again
Needs to eventually stop the loop
What does “iteration” mean in the context of loops?
One full pass/run of the code block inside of the loop
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?
Before each iteration
When does the condition expression of a for loop get evaluated?
At the beginning of each subsequent iteration
When does the final expression of a for loop get evaluated?
The end of each loop iteration and before the next evaluation of condition.
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 one to the variable
How do you iterate through the keys of an object?
For in loop
Give two examples of document methods that retrieve a single element from the DOM.
QuerySelector and GetElementById
Give one example of a document method that retrieves multiple elements from the DOM at once.
GetElementsByTagName
Why might you want to assign the return value of a DOM query to a variable?
So we don’t have to consciously query the dom
What console method allows you to inspect the properties of a DOM element object?
.dir
What does document.querySelector() take as its argument and what does it return?
Takes an element, id, class (any valid css selector)
Returns the first instance of that selector
What does document.querySelectorAll() take as its argument and what does it return?
Take a css selectors
Returns a NodeList
Why do we log things to the console?
To confirm the data as we write the code
What is the purpose of events and event handling?
Have code run based on when things happen
What method of element objects lets you set up a function to be called when a specific type of event occurs?
Add event listener
What is a callback function?
Any function that we do not call directly
What object is passed into an event listener callback when the event fires?
Event object
It gets named event or e but technically can have any name
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Point of origin for the event
Check MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
1st one just has a function for the handle click
2nd has a callback function for the handle click
What is the className property of element objects?
Class Attribute value
How do you update the CSS class attribute of an element using JavaScript?
Classname.property
What is the textContent property of element objects?
Represents the Content of the node and it’s descendants
How do you update the text within an element using JavaScript?
Query the dom and then use .textContent
Is the event parameter of an event listener callback always useful?
No, if the function doesn’t need the callback
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?
Calling dom over and over is taxing on the browser
And we don’t want to rely on text content
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 event
What does the event.preventDefault() method do?
Prevents the browser from automatically reloading with the submitted content
What does submitting a form without event.preventDefault() do?
The list events will remain on the html page
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?
The errors can compound and it gets even harder to fix later
What is an advantage of having your console open when writing a JavaScript program?
To quickly see and test what you’re coding and find errors
Does the document.createElement() method insert a new element into the page?
No, it creates a new element, but doesn’t put it into the page
How do you add an element as a child to another element?
parentName.appendChild(childName)
What do you pass as the arguments to the element.setAttribute() method?
element.setAttribute(name, attribute)
What steps do you need to take in order to insert a new element into the page?
Use document.createElement(), specify the type of child element you want to make and assign it to a variable. Choose a parent element that the variable will be appended to. Use quereySelector and assign it to a variable. Then use .appendChild to append the child variable to the parent variable.
What is the textContent property of an element object for?
To get the text content an html element has
Name two ways to set the class attribute of a DOM element.
.className and .setAttribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Readability
Repeat a process over and over
Give two examples of media features that you can query in an @media rule.
Width, resolution, hover, height
What is a method?
A function which is a property of an object
How can you tell the difference between a method definition and a method call?
Definitions include a code block and call is just the object dot method name.
Describe method definition syntax (structure).
Define a variable
Assign it with an object literal
Add properties with functions
Describe method call syntax (structure).
ObjectName.methodName()
How is a method different from any other function?
A method is part of an object
What is the defining characteristic of Object-Oriented Programming?
Creating objects that both store data and functionality.
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
Simplifying complex concepts/ideas
Hiding complex functionality behind simple functionality
What does API stand for?
Application Programming Interface
What is the purpose of an API?
Allows users to use abstraction of software
To share functionality of an application to other users
What is this in JavaScript?
An implicit parameter that refers to the object that exists at call time
What does it mean to say that this is an “implicit parameter”?
A parameter that is available in a function’s code block even though it was never explicitly declared
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); } };
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
because it’s referring to the window object and the window object doesn’t have a firstName property
How can you tell what the value of this will be for a particular function or method definition?
The object containing this
How can you tell what the value of this is for a particular function or method call?
Left of the dot at the method call.
What kind of inheritance does the JavaScript programming language use?
Prototypal inheritance
What is a prototype in JavaScript?
An object that contains properties and 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 prototype has the methods and the strings and arrays can use the methods
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
The prototype chain
What does the new operator do?
- Creates a new blank JS object
- Adds a property of proto to it
- All the passed in parameters are bound to the new object with the this keyword
- Returns the object
What property of JavaScript functions can store shared behavior for instances created with new?
Inheritance
What does the instanceof operator do?
Checks if the passed in created object comes from the passed in constructor
What is a “callback” function?
A function that is passed to 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 seconds
What do setTimeout() and setInterval() return?
timeoutId
What is a client?
The one that makes the request to get info/services from a server
What is a server?
Wait for requests to come in and answer requests/provide services
Which HTTP method does a browser issue to a web server when you visit a URL?
GET request
What three things are on the start-line of an HTTP request message?
Method, request type, version that is coming in
What three things are on the start-line of an HTTP response message?
Protocol version, status code, text
What are HTTP headers?
Let’s you pass additional information related to the request
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN documentation
Is a body required for a valid HTTP request or response message?
No
What is AJAX?
JQuerey libaray that lets you make asynchronous HTTP requests to make a scynchronous functionality for your webpages
What does the AJAX acronym stand for?
Asynchonous Javascript and XML
Which object is built into the browser for making HTTP requests in JavaScript?
XHR
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
Load event
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
By using Prototype
What is a code block? What are some examples of a code block?
A local scope of code, where variables declared inside only can work inside (anything inside curly braces
If statements, while loops
What does block scope mean?
Whatever is defined inside of the block, only exists inside of the block
What is the scope of a variable declared with const or let?
Block-scoped
What is the difference between let and const?
Let can be reassigned a value but const is immutable and cannot be changed
Why is it possible to .push() a new value into a const variable that points to an Array?
The memory address of the array is the same, changing the array values, doesn’t alter this
How should you decide on which type of declaration to use?
If you’re gonna have a changing variable, use let, otherwise const (default to const)
What is destructuring, conceptually?
A way to create variables from object properties or indexes in the array immediately in one line
What is the syntax for Object destructuring?
Const {propertyVariable} = objectName;
What is the syntax for Array destructuring?
Const [indexVariable] = arrayName;
How can you tell the difference between destructuring and creating Object/Array literals?
{} on the right of the = is creating
{} on the left of the = is destructuring
What is the syntax for writing a template literal?
Back tick, string content, ${variableName}, back tick
What is “string interpolation”?
When you can directly bring the value of a variable into a string
What is the syntax for defining an arrow function?
Parameter(s) => arguments
When an arrow function’s body is left without curly braces, what changes in its functionality?
It has an implicit return statement
Having curly brackets makes it work like a standard function block
How is the value of this determined within an arrow function?
They treat this as it is when it is defined
What kind of inheritance does the JavaScript programming language use?
Prototypal inheritance
What is a prototype in JavaScript?
An object that contains properties and 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 prototype has the methods and the strings and arrays can use the methods
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
The prototype chain
What does the new operator do?
- Creates a new blank JS object
- Adds a property of proto to it
- All the passed in parameters are bound to the new object with the this keyword
- Returns the object
What property of JavaScript functions can store shared behavior for instances created with new?
Inheritance
What does the instanceof operator do?
Checks if the passed in created object comes from the passed in constructor
What is a “callback” function?
A function that is passed to 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 seconds
What do setTimeout() and setInterval() return?
timeoutId
What is a client?
The one that makes the request to get info/services from a server
What is a server?
Wait for requests to come in and answer requests/provide services
Which HTTP method does a browser issue to a web server when you visit a URL?
GET request
What three things are on the start-line of an HTTP request message?
Method, request type, version that is coming in
What three things are on the start-line of an HTTP response message?
Protocol version, status code, text
What are HTTP headers?
Let’s you pass additional information related to the request
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN documentation
Is a body required for a valid HTTP request or response message?
No
What is AJAX?
JQuerey libaray that lets you make asynchronous HTTP requests to make asynchronous functionality for your webpages
What does the AJAX acronym stand for?
Asynchonous Javascript and XML
Which object is built into the browser for making HTTP requests in JavaScript?
XHR
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
Load event
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
By using Prototype
What is a code block? What are some examples of a code block?
A local scope of code, where variables declared inside only can work inside (anything inside curly braces
If statements, while loops
What does block scope mean?
Whatever is defined inside of the block, only exists inside of the block
What is the scope of a variable declared with const or let?
Block-scoped
What is the difference between let and const?
Let can be reassigned a value but const is immutable and cannot be changed
Why is it possible to .push() a new value into a const variable that points to an Array?
The memory address of the array is the same, changing the array values, doesn’t alter this
How should you decide on which type of declaration to use?
If you’re gonna have a changing variable, use let, otherwise const (default to const)
What is destructuring, conceptually?
A way to create variables from object properties or indexes in the array immediately in one line
What is the syntax for Object destructuring?
Const {propertyVariable} = objectName;
What is the syntax for Array destructuring?
Const [indexVariable] = arrayName;
How can you tell the difference between destructuring and creating Object/Array literals?
{} on the right of the = is creating
{} on the left of the = is destructuring
What is the syntax for writing a template literal?
Back tick, string content, ${variableName}, back tick
What is “string interpolation”?
When you can directly bring the value of a variable into a string
What is the syntax for defining an arrow function?
Parameter(s) => arguments
When an arrow function’s body is left without curly braces, what changes in its functionality?
It has an implicit return statement
Having curly brackets makes it work like a standard function block
How is the value of this determined within an arrow function?
They treat this as it is when it is defined