Javascript Flashcards
What is the purpose of variables?
To temporarily store a value in a program
How do you declare a variable?
with the var keyword
How do you initialize (assign a value to) a variable?
with the assignment operator followed by a value
What characters are allowed in variable names?
letters, numbers(but not first), underscores, and dollar signs.
What does it mean to say that variable names are “case sensitive”?
each letter, undercase and lowercase, has a different ASCII value.
What is the purpose of a string?
to store words and shit
What is the purpose of a number?
to store number
What is the purpose of a boolean?
to store either a true or false value that will determine the outcome of certain decisions in your program.
What does the = operator mean in JavaScript?
it is the assignment operator that will assign a value to a variable.
How do you update the value of a variable?
by initializing it with no var keyword.
What is the difference between null and undefined?
null is an assigned blank value while undefined is a not yet defined value.
Why is it a good habit to include “labels” when you log values to the browser console?
it makes code reading easier
Give five examples of JavaScript primitives.
number, string, boolean, null, undefined
What data type is returned by an arithmetic operation?
a number
What is string concatenation?
combining strings with an addition sign
What purpose(s) does the + plus operator serve in JavaScript?
both adds numbers and concatenates strings
What data type is returned by comparing two values (, ===, etc)?
a boolean
What does the += “plus-equals” operator do?
it adds the value, or expression, to the right of the equals sign to value of the variable to the left and then assigns that value to the variable.
What are objects used for?
to store related data and put together a type of model of some sort.
What are object properties?
variables inside of an object
Describe object literal notation.
after var assignment opening curly brace followed by the key of the property then a colon then the value of that property. Each of those properties are separated by commas. End with a closing curly brace.
How do you remove a property from an object?
the delete keyword and the property
What are the two ways to get or update the value of a property?
dot notation and bracket notation
What are arrays used for?
for lists that don’t need to be ordered
Describe array literal notation.
after var assignment opening bracket then values separated by commas then closing bracket
How are arrays different from “plain” objects?
they use the index system instead of keys.
What number represents the first index of an array?
0
What is the length property of an array?
array.length and it gives the length of the array.
How do you calculate the last index of an array?
array.length - 1.
What is a function in JavaScript?
A set of code that is declared to complete a specific task
Describe the parts of a function definition.
the function key word, the name of the function, parameters if any, then the code block
Describe the parts of a function call.
the function name followed by arguments if any then a semi colon.
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function call is when a function is to be used, the function definition is when a function is to be declared.
What is the difference between a parameter and an argument?
a parameter is the place holder variable in the function definition that will replaced when the function is called by the arguments listed in the function call. arguments are in the function call.
Why are function parameters useful?
the way to provide data for the functions and give different results so that the functions can be used in a variety of cases.
What two effects does a return statement have on the behavior of a function?
it both returns data to the function then stops the function.
Why do we log things to the console?
for debugging purposes to catch mistakes.
What is a method?
a function attached to an object
How is a method different from any other function?
it directly modifies the thing it is attached to. other than that they are the same
How do you remove the last element from an array?
using the pop method
How do you round a number down to the nearest integer?
using the math object with the floor method
How do you generate a random number?
using the math object with the random method
How do you delete an element from an array?
using the splice method on your array.
How do you append an element to an array?
using the push method on your array
How do you break a string up into an array?
using the split method with a string of a single space as your argument
Do string methods change the original string? How would you check if you weren’t sure?
no they only affect the return value. You can check by logging the original string to the console.
Is the return value of a function or method useful in every situation?
no, you don’t need to use it if you don’t have to.
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?
to give multiple paths for a conditional statement
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
the key word if followed by the conditional expression and then the code block
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
with logical operators.
What is the purpose of a loop?
to repeat a certain section of code if necessary
What is the purpose of a condition expression in a loop?
to give that loop an exit route.
What does “iteration” mean in the context of loops?
how many times the loop runs
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?
its the very first step
When does the condition expression of a for loop get evaluated?
before each loop iteration runs
When does the final expression of a for loop get evaluated?
after the loop code block is excecated
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 and assigns it to the variable
How do you iterate through the keys of an object?
a for in loop
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?
Objects in the JavaScript language
What is a DOM Tree?
a tree representation of an HTML document
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?
because if used more than once it doesn’t have to retrieve the return address of a DOM query multiple times.
What console method allows you to inspect the properties of a DOM element object?
dir
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so all the HTML could run first
What does document.querySelector() take as its argument and what does it return?
takes a string of the tag name, class, or id as a CSS selector. It returns the first element it finds that matches that selector
What does document.querySelectorAll() take as its argument and what does it return?
takes a string of a CSS selector. It returns a node list of all the elements under that selector
What is the purpose of events and event handling?
It is a way for the user to interact with the webpage and potentially change the style of the page and many other things.
What do [] square brackets mean in function and method syntax documentation?
its 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 callback function is a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
an event object built by JavaScript.
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It is the element object from where the event originated from. you could console.log the event object and check, or look it up in MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
the first waits to execute the function until that element node is clicked, while the second one executes before the webpage is loaded.
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?
by using the className property on the event variable and setting it equal to the class name in a string.
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?
by using the textContent property on the event variable and setting it equal to the text you desire
Is the event parameter of an event listener callback always useful?
no
Why is storing information about a program in variables better than only storing it in the DOM?
It cuts back execution time because JavaScript doesn’t constantly have to fetch all the elements it needs to use, it just uses the stored addresses in the variable assigned.
What event is fired when a user places their cursor in a form control?
the focus event
What event is fired when a user’s cursor leaves a form control?
the blur event
What event is fired as a user changes the value of a form control?
the input event
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?
it refreshes the 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 an advantage of having your console open when writing a JavaScript program?
You can see when and where mistakes happen
What event is fired when a user clicks the “submit” button within a ?
the submit event
Does the document.createElement() method insert a new element into the page?
no it just creates an element
How do you add an element as a child to another element?
appendChild method
What do you pass as the arguments to the element.setAttribute() method?
the attribute name and the value
What steps do you need to take in order to insert a new element into the page?
creating the element, query the element you want to append it to, then append the element you created to the node you queried.
What is the textContent property of an element object for?
to either retrieve the text, or assign the element some text value
Name two ways to set the class attribute of a DOM element.
use the setAttribute method, or assign a new name to the className property.
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
it is both reusable and condenses code.
What is the affect of setting an element to display: none?
you can no longer see nor interact with said element and it is no longer in document flow.
What does the element.matches() method take as an argument and what does it return?
it takes a string of a CSS selector and returns a boolean of whether or not the selector is the same value
How can you retrieve the value of an element’s attribute?
by using the 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?
using an eventListener on every node in that tab that needs to be added
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?
comparing each class name at the index starting from 0 to the length of the node list one by one manually.
What is JSON?
JSON is a text-based data format following JavaScript object syntax
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
You can do stuff like store it on disk or send it over the network.
How do you serialize a data structure into a JSON string using JavaScript?
using the JSON.stringify method with the data as the argument
How do you deserialize a JSON string into a data structure using JavaScript?
with the JSON.parse method with the JSON string as the argument.
How to you store data in localStorage?
by using the setItem method on the localStorage object
How to you retrieve data from localStorage?
by using the getItem method on the localStorage object
What data type can localStorage save in the browser?
only strings or null
When does the ‘beforeunload’ event fire on the window object?
right before the webpage is either reloaded or exited.
How can you tell the difference between a method definition and a method call?
method definitions use the function keyword and are ended with a code block for defining the function. method calls use that method of the property it is attached to followed by parentheses and arguments in that if needed.
Describe method definition syntax (structure).
method definitions have the function keyword, followed by the function name, followed by any parameters within parentheses, followed by the definition block
Describe method call syntax (structure).
method calls are attached to the property it is used on and are followed by parameters incased in parentheses if any.
How is a method different from any other function?
a method can be used on objects while functions cannot.
What is the defining characteristic of Object-Oriented Programming?
That objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, and Polymorphism.
What is “abstraction”?
Being able to work with (possibly) complex things in simple ways. Basically making complex problems easier to tackle.
What does API stand for?
Application Programming Interface
What is the purpose of an API?
It gives programmers a way to interact with a system in a simplified, consistent fashion
What is this in JavaScript?
this is an implicit parameter of all JavaScript functions.
What does it mean to say that this is an “implicit parameter”?
it means that the values change based on the context
When is the value of this determined in a function; call time or definition time?
during the function call
How can you tell what the value of this will be for a particular function or method definition?
you cannot
How can you tell what the value of this is for a particular function or method call?
by the object that it is being used on
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?
prototypes
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
__proto__ object
What does the new operator do?
creates a blank 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, and returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
the prototype property
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
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 function
How can you set up a function to be called repeatedly without using a loop?
setInterval function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0ms
What do setTimeout() and setInterval() return?
a timeoutID to later use with clearTimeout functions and shit
What is AJAX?
Ajax is a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest.
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
the XMLHttpRequest object
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load event
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
through the power of PROTOTYPES
What is a code block? What are some examples of a code block?
a code block is a set of instructions contained within a set of curly braces. { console.log(‘you are super cute’) }
What does block scope mean?
the accessible code within that code block
What is the scope of a variable declared with const or let?
it can be used within the code block it was declared in
What is the difference between let and const?
let is mutable while const is not mutable
Why is it possible to .push() a new value into a const variable that points to an Array?
because the const value is holding the address of the array, not to contents inside of it.
How should you decide on which type of declaration to use?
if you want the value of the variable to change or not.
What is the syntax for writing a template literal?
using backticks instead of single or double quotes for strings
What is “string interpolation”?
adding expressions inside of strings.
What is destructuring, conceptually?
assigning properties to variables
What is the syntax for Object destructuring?
const { property1, property2 } = object;
What is the syntax for Array destructuring?
const [property1, property2 ] = array;
How can you tell the difference between destructuring and creating Object/Array literals?
the “assignment” of the object name is after the object or array literal.
What is the syntax for defining an arrow function?
parameters , arrow syntax , definition
When an arrow function’s body is left without curly braces, what changes in its functionality?
you don’t need a return statement
How is the value of this determined within an arrow function?
in an ES5 regular function, the value of this is determined at call time. when it is called, this is the object to the left of the method. If no object it is the window object. to sum up, the value of this in arrow functions is determined at definition time, while in ES5 functions, the value of this is defined at execution time.
What method is available in the Node.js fs module for writing data to a file?
fs.writeFile( );
Are file operations using the fs module synchronous or asynchronous?
asynchronous
What are the three states a Promise can be in?
pending, fulfilled, rejected
How do you handle the fulfillment of a Promise?
by using the then method of the Promise object
How do you handle the rejection of a Promise?
by using the catch method of the Promise object
What is the typeof an ES6 class?
an object
Describe ES6 class syntax.
first start with the
What is “refactoring”?
changing the way how code is written, not tampering with the functionality, but making its readability easier and such.
What does fetch() return?
it returns a promise containing a response
What is the default request method used by fetch()?
GET?
How do you specify the request method (GET, POST, etc.) when calling fetch?
as the method property of the init object passed as a second optional argument in the fetch function
When does React call a component’s componentDidMount method?
immediately after render
Name three React.Component lifecycle methods.
componentDidMount, componentUpdate, componentWillMount
How do you pass data to a child component?
as a prop