Javascript Flashcards
What is the purpose of a number?
To assign numerical value
What is the purpose of a boolean?
To determine if something is true or false
What does the = operator mean in JavaScript?
It says that you are going to assign a value to a variable
How do you update the value of a variable?
By recalling the variable by the name and using the assignment operator to assign it a new value
What is the difference between null and undefined?
null is an assigned value. It means nothing.(intentional empty) undefined means a variable has been declared but not defined yet
Why is it a good habit to include “labels” when you log values to the browser console?
to help you debug, but if you do not include “labels”, it can be very confusing instead of helpful
Give five examples of JavaScript primitives.
string, number, boolean, undefined, and null.
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Taking two strains and putting them together
What purpose(s) does the + plus operator serve in JavaScript?
To add values and concatenate
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left
What are arrays used for?
To store a list of values
Describe array literal notation.
The values are assigned to the array inside a pair of square brackets, and each value is separated by a comma.
How are arrays different from “plain” objects?
It holds a list of related values in order, white object holds list of values out of order
What number represents the first index of an array?
0
What is the length property of an array?
It holds the number of items in the array
How do you calculate the last index of an array?
Subtracting 1 from the length of an array
What are objects used for?
To group together a set of variables and functions to create a model
What are object properties?
Variables that are part of an object.
Describe object literal notation.
Object is contained by curly braces,
You separate each key from its value by using a colon, separate each property and method with a comma.
How do you remove a property from an object?
Use keyword delete operator followed by dot notation
What are the two ways to get or update the value of a property?
Using dot notation or brackets notation
What is a function in JavaScript?
Functions allow you to package up code for use later in your program.
Describe the parts of a function definition:
Function keyword to bring a new function
An optional name
A comma-separated list of zero or more parameters
The start of the code block enclosed by curly braces
An optional return statement
Describe the parts of a function call.
The function’s name and a comma-separated list of zero or more arguments surrounded by ()
When comparing them side-by-side, what are the differences between a function call and a function definition?
Call, recalls the function and executes the code to run, definition defines and states what the code is going to do
What is the difference between a parameter and an argument?
Parameters are used to define a function, arguments are used to call a function.
Why are function parameters useful?
Because they allow to function to be more flexible
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a value we use in our program
Prevents any more code in the code block from being run
Why do we log things to the console?
To use as a debugging tool and learning to play and learn about JS
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 associated with an object while a function is not
How do you remove the last element from an array?
pop()
How do you round a number down to the nearest integer?
floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array?
splice()
How do you append an element to an array?
push() unshift()
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 they don’t change original string, assigning original string to a var and checking with new string
Roughly how many string methods are there according to the MDN Web docs?
60+ methods
Is the return value of a function or method useful in every situation?
no
Roughly how many array methods are there according to the MDN Web docs?
30+ methods
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.
> , < , >= , <= , === , and !==
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
guides a program to make decisions based on specified criteria
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
If ( exp ) {
Code block to run
}
What are the three logical operators?
And or not
How do you compare two different expressions in the same condition?
With and / or logical operators
What is the purpose of a loop?
To perform repeated tasks based on a condition
What is the purpose of a condition expression in a loop?
Tells the loop when to stop
What does “iteration” mean in the context of loops?
Repetition of a function or process
When does the condition expression of a while loop get evaluated?
before executing the statement. And one after
When does the initialization expression of a for loop get evaluated?
Before each loop iteration, just once
When does the condition expression of a for loop get evaluated?
Each time before the loop runs
When does the final expression of a for loop get evaluated?
At the end of each loop iteration.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break statement
What does the ++ increment operator do?
Increase the number by 1 and reassigns the value back into variable
How do you iterate through the keys of an object?
Using a for in loop
Why do we log things to the console?
To debug and make sure things are working.
What is a “model”?
The representation of something
Which “document” is being referred to in the phrase Document Object Model?
The document which is being represented on the web page
What is the word “object” referring to in the phrase Document Object Model?
Its referring to each different part of the page being loaded in the window
What is a DOM Tree?
It is the model of a web page
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.
getElementsbyClassName
Why might you want to assign the return value of a DOM query to a variable?
It helps page speed, by saving the reference location of the value in the Dom tree.
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?
To help the page load faster by loading the html elements first.
What does document.querySelector() take as its argument and what does it return?
Css selector syntax and returns only the first of the matching elements
What does document.querySelectorAll() take as its argument and what does it return?
Css selector syntax and returns all of those that match
What event is fired when a user places their cursor in a form control?
Focus event
What event is fired when a user’s cursor leaves a form control?
Blur event
What event is fired as a user changes the value of a form control?
Form event
What event is fired when a user clicks the “submit” button within a ?
Submit event
What does the event.preventDefault() method do?
Cancel default behavior of event
What does submitting a form without event.preventDefault() do?
Reload the page with its return values in the address bar
What property of a form control object gets and sets its value?
Value attribute
What is one risk of writing a lot of code without checking to see if it works so far?
Bugs and issues / broken code
What is an advantage of having your console open when writing a JavaScript program?
You are able to check if any code is broken or if anything is not showing correct output
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?
AddEventListerner Function
What is a callback function?
Is any function that is passed to another function but is not called.
What object is passed into an event listener callback when the event fires?
Event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Is the element which dispatches the element. MDN
What is the className property of element objects?
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?
element.className = “class”;
What is the textContent property of element objects?
Property that stores the text content of the element
How do you update the text within an element using JavaScript?
.textcontent =
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, we would have to access the Dom as the source of truth.
Why is storing information about a program in variables better than only storing it in the DOM?
Because we can refer back it later and reuse as many times as we need.
Does thedocument.createElement()method insert a new element into the page?
no
- How do you add an element as a child to another element?
appendChild()
- What do you pass as the arguments to theelement.setAttribute()method?
name to attribute, value
What steps do you need to take in order to insert a new element into the page?
appendChild that new element to its parent
- What is thetextContentproperty of an element object for?
the text content and all its decendaces
- Name two ways to set theclassattribute of a DOM element. .
className, setAttribute()
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
that it is repeatable to work with data structures, the availability to separate function so it doesn’t affect anything else in the program , incase we need to change data at a later point
What is the event.target?
The target event propertyreturns the element that triggered the event
Why is it possible to listen for events on one element that actually happen its descendent elements?
Because of event flow
What DOM element property tells you what type of element it is
nodeName / Tagname
What does the element.closest() method take as its argument and what does it return?
How can you remove an element from the DOM?
returnsthe first ancestor of the selected element
Takes a selector as the argument
With element.remove() function
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?
By adding an event listener to its parent element
What is the event.target?
Target of the event being interacted with
What is the affect of setting an element to display: none?
It hides all the content within that element
What does the element.matches() method take as an argument and what does it return?
A selector as its argument and returns true or false value
How can you retrieve the value of an element’s attribute?
getAttribute() method
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 individual addeventlisteners for each elemnt
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?
Using an if statement if one tab is ‘showing’ hide all other tabs
What is JSON?
JSON is a lightweight format for storing and transporting data.
What are serialization and deserialization?
Serialization is a mechanism of converting the state of an object into a byte stream
Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.
Why are serialization and deserialization useful?
They help to sa¥ve file¥¥s onto the local disk or sent over the network to any other machine
How do you serialize a data structure into a JSON string using JavaScript?
stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( string, function )
How do you store data in localStorage?
localStorage.setItem() KEY, VALUE
How do you retrieve data from localStorage?
localStorage.getItem()
What data type can localStorage save in the browser?
JSON Data
When does the ‘beforeunload’ event fire on the window object?
is fired when the window, the document and its resources are about to be unloaded.
What is a method?
Is a function which is a property of an object
Describe method definition syntax (structure).
Object {method: function() {} }
Describe method call syntax (structure).
Object.method()
How is a method different from any other function?
Difference is that 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?
its purpose is to facilitate a way for two or more computer programs to communicate with each other.
What is this in JavaScript?
the this keyword refers to an object. Which object depends on how this is being invoked (used or called).
What does it mean to say that this is an “implicit parameter”?
meaning that it is available in a function’s code block even though it was never included in the function’s parameter list or declared withvar
When is the value of this determined in a function; call time or definition time?
Call time
What doesthisrefer to in the following code snippet? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
Object character
Given the abovecharacterobject, what is the result of the following code snippet? Why?
It returns the greet function inside character object, because this is being referred to character object
Given the above character object, what is the result of the following code snippet? Why?
It returns It’s-a-me, undefined!, because the function if not being called.
How can you tell what the value of this will be for a particular function or method definition?
By seeing what object the this method is inside of
How can you tell what the value of this is for a particular function or method call?
By looking at what is left of the period
What kind of inheritance does the JavaScript programming language use?
Prototype based inheritance
What is a prototype in JavaScript?
The prototype is an object that is associated with every functions and objects by default in JavaScript
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
Because of prototypes property’s they inherit.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
In its prototype chain
What does the new operator do?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function
What does the instanceof operator do?
is used to check the type of an object at the run time. It returns a boolean value(true or false).
What is a “callback” function?
Acallback functionis 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?
By using the setTimeout function
How can you set up a function to be called repeatedly without using a loop?
Using the setinterval function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
A unique intervalID
What is a client?
a client is any computer hardware or software device that requests access to a service provided by a server
What is a server?
A server isa computer program or device that provides a service to another computer program and its user, also known as the client.
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
What three things are on the start-line of an HTTP request message?
An HTTP method, a verb (like GET , PUT or POST )
The request target, usually a URL,
HTTP version
What three things are on the start-line of an HTTP response message?
The protocol version, usually HTTP/1.1 .
A status code, indicating success or failure of the request. Common status codes are 200 , 404 , or 302.
A status text.
What are HTTP headers?
HTTP headers let the client and the server pass additional information with an HTTP request or response.
Is a body required for a valid HTTP request or response message?
no, requests fetching resources, likeGET,HEAD,DELETE, orOPTIONS, usually don’t need one.
What is AJAX?
Is 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
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?
They both share a prototype object, prototype inheritance