javascript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
A variable keyword and then the variable name
How do you initialize (assign a value to) a variable?
Using the assignment operator (=)
What characters are allowed in variable names?
Letters, numbers, dollar sign, or an underscore
What does it mean to say that variable names are “case sensitive”?
You can reuse the same name with different capitalization
What is the purpose of a string?
Holding data in text form
What is the purpose of a number?
For tasks involving counting or calculating and to store numerical data
What is the purpose of a boolean?
To store data as either a yes or no (lightswitch); yes it should do this or no it should not
What does the = operator mean in JavaScript?
Assignment operator; Assign a value to a variable
How do you update the value of a variable?
Using the assignment operator
What is the difference between null and undefined?
Undefined is a type while null is an object; Undefined is a variable that has been declared but the value has not
Why is it a good habit to include “labels” when you log values to the browser console?
To help keep track of what you are console logging
Give five examples of JavaScript primitives.
String, number, undefined, null, boolean
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
Combining two strings
What purpose(s) does the + plus operator serve in JavaScript?
To add numbers and concatenate strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Concatenates and assigns the new value to the original variable
What are objects used for?
To store properties and their values. They are meant to model real world objects.
What are object properties?
They tell us about the object (such as the name of a hotel)
Describe object literal notation.
You create an object by defining the key value pairs and assign it to a variable var student = { name = Shawn }
How do you remove a property from an object?
Using the 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?
Lists
Describe array literal notation.
Using the square brackets
How are arrays different from “plain” objects?
Arrays are indexed numerically
What number represents the first index of an array?
0
What is the length property of an array?
Shows you numerically the length of the array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
Combining multiple lines of code into a function for ease of access and readability
Describe the parts of a function definition.
The function keyword along with an optional name and a parameter surrounded by parenthesis and then an opening and closing curly brace
Describe the parts of a function call.
The functions name with an argument surrounded by parenthesis
When comparing them side-by-side, what are the differences between a function call and a function definition?
The parameter becomes an argument and you don’t need the curly braces
What is the difference between a parameter and an argument?
A parameter is defined with the function, while an argument is passes when calling a function
Why are function parameters useful?
To store and pass data and provide extra information about the function
What two effects does a return statement have on the behavior of a function?
Causes the code block within the function to be ran, and prevents more code from being run.
Why do we log things to the console?
So we can see what’s actually happening with our code.
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
How do you remove the last element from an array?
Pop method
How do you round a number down to the nearest integer?
Round method
How do you generate a random number?
Math.random method
How do you delete an element from an array?
Splice method
How do you append an element to an array?
Push method
How do you break a string up into an array?
Split method
Do string methods change the original string? How would you check if you weren’t sure?
No; console log
Roughly how many string methods are there according to the MDN Web docs?
50
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?
37
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 make decisions on which code to run
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
Keyword, then condition, then opening curly brace, the code you want to run, and then a closing curly brace.
What are the three logical operators?
&&; ||; ! (and, or, logical not)
How do you compare two different expressions in the same condition?
Logical operators
What is the purpose of a loop?
To keep checking conditions until a statement is true.
What is the purpose of a condition expression in a loop?
Setting how long the loop should run
What does “iteration” mean in the context of loops?
Each time a loop runs
When does the condition expression of a while loop get evaluated?
Before the statement is executed
When does the initialization expression of a for loop get evaluated?
Before the condition
When does the condition expression of a for loop get evaluated?
After the initialization and before the final-expression
When does the final expression of a for loop get evaluated?
After the 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?
Increase by 1
How do you iterate through the keys of an object?
For-in loops
Why do we log things to the console?
So we can see what’s actually happening.
What is a “model”?
A recreation of something
Which “document” is being referred to in the phrase Document Object Model?
The current html document you are working on
What is the word “object” referring to in the phrase Document Object Model?
Represents the different objects within the DOM
What is a DOM Tree?
A model of a web page
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?
To store the location of the element. This way the DOM doesn’t have to search through 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?
It needs to be loaded after all the HTML content
What does document.querySelector() take as its argument and what does it return?
It takes a css selector and returns only the first of the matching elements
What does document.querySelectorAll() take as its argument and what does it return?
It takes a css selector and returns all elements that match
Why do we log things to the console?
To see what data is being passed
What is the purpose of events and event handling?
To run code based off of things that happen on the web page
What do [] square brackets mean in function and method syntax documentation?
It means the argument is 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 function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
The event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The place where the event is being invoked, and in the debugger.
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The second one will run as soon as it hits the line of code, instead of when the event occurs
What is the className property of element objects?
the string within the class attribute
How do you update the CSS class attribute of an element using JavaScript?
Using the className property
What is the textContent property of element objects?
the text content of the node and its decendants
Is the event parameter of an event listener callback always useful?
Useful but not necessary
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
Harder
Why is storing information about a program in variables better than only storing it in the DOM?
So you can access it again quickly and so you don’t depend on the DOM.
How do you update the text within an element?
Select the element and use the textContent property
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 the default event of whatever it’s in
What does submitting a form without event.preventDefault() do?
Reloads the page after you submit
What property of a form element object contains all of the form’s controls.
.elements property
What property of a form 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?
It makes it a lot harder to debug
What is an advantage of having your console open when writing a JavaScript program?
You can see what’s happening at all times
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?
appendChild() method
What do you pass as the arguments to the element.setAttribute() method?
The attribute name then the value
What steps do you need to take in order to insert a new element into the page?
Create the element, give it content, and add it to the dom
What is the textContent property of an element object for?
To change the text content of that element
Name two ways to set the class attribute of a DOM element.
setAttribute() and className() methods
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can reuse the function later & so you don’t repeat yourself
What is the event.target?
The element interacted with during an event
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event flow makes it possible. You are clicking on an element that is within another element (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?
It takes the selector and returns itself or the closest element to the selector
How can you remove an element from the DOM?
The remove method. element.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 an event listener to the parent element
What is the event.target?
The element interacted with during an event
What is the affect of setting an element to display: none?
The document renders as if the element doesn’t exist
What does the element.matches() method take as an argument and what does it return?
It takes a selector, and returns a boolean
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 steps
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?
Multiple event listeners
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?
Multiple conditional statements
What is JSON?
Text-based data format following JavaScript object syntax
What are serialization and deserialization?
Deserialization is when you convert a string to a native object, and serialization is when you convert an object to a string.
Why are serialization and deserialization useful?
It allows us to transmit data
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?
Parse
How to you store data in localStorage?
setItem() method
How to you retrieve data from localStorage?
getItem() method
What data type can localStorage save in the browser?
String
When does the ‘beforeunload’ event fire on the window object?
Before everything is dumped out of the memory from the browser.
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?
A method definition is when you give a method functionality whereas a method call you are returning the functionality of that property onto an object.
Describe method definition syntax (structure).
You define a method like you would an object property and then give it the function to run whatever code you want.
Describe method call syntax (structure).
object.Method(). You attach the method to the object and return the result.
How is a method different from any other function?
A method is a property of an object
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?
Give 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”?
that it 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 character object
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It returns the message variable because it runs the greet method because the greet method is being called on the character object,
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
It returns the message + undefined because this.firstName isn’t defined within the variable hello. This keyword is now attached to hello rather than the firstName property of the character object.
How can you tell what the value of this will be for a particular function or method definition?
By default it will refer to the object in which it was created
How can you tell what the value of this is for a particular function or method call?
To whatever object calls the function
What kind of inheritance does the JavaScript programming language use?
prototype-based or prototypal
What is a prototype in JavaScript?
a JavaScript prototype is simply 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?
Those methods are defined on a “prototype” object and arrays simply borrow those methods when they’re needed.
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 blank, plain JavaScript object;
Links (sets the constructor of) the newly created object to another object by setting the other object as its parent prototype;
Passes the newly created object from Step 1 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
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. The return value is a boolean value.
What is a “callback” function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
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 timeout or interval ID which is an integer
What is a code block? What are some examples of a code block?
Grouped statements within a curly brace, if else, for, while
What does block scope mean?
It refers to the lines of code within the code block
What is the scope of a variable declared with const or let?
Block scope
What is the difference between let and const?
You can reassign values to variables with let, while you can’t do that with const
Why is it possible to .push() a new value into a const variable that points to an Array?
Because you’re adding to the array rather than re-assigning or re-declaring
How should you decide on which type of declaration to use?
You should use const unless you need to reassign the variable later
What is destructuring, conceptually?
Unpacking values into variables
What is the syntax for Object destructuring?
const {name, name, name} = objectVariable
What is the syntax for Array destructuring?
const [name, name, name] = arrayVariable
How can you tell the difference between destructuring and creating Object/Array literals?
If it’s happening on the left side of the equal sign its destructuring
What is the syntax for writing a template literal?
${variableName}
What is “string interpolation”?
The ability to substitute part of the string for the values of variables or expressions.
What is the syntax for defining an arrow function?
const = (p1, p2, …, pn) => expression
or
const = (p1,p2,..,pn) => {}
When an arrow function’s body is left without curly braces, what changes in its functionality?
It doesn’t need a return statement
How is the value of this determined within an arrow function?
This will be referring to the enclosing context, rather than creating its own context within the function
What is a CLI?
Command line interface
What is a GUI?
Graphical user interface
man
Brings up the manual in the terminal
cat
Print contents of a file in the terminal
ls
List contents of a directory/file in the terminal
pwd
Shows your working directory in the terminal
echo
Add content to a file
touch
Creates a new file
mkdir
Creates a new directory
mv
Rename a directory
rm
Remove a file
cp
Copy a file
What are the three virtues of a great programmer?
Laziness, patience, hubris
What is Node.js?
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.
What can Node.js be used for?
back end services
What is a REPL?
Read-eval-print-loop; used for single line commands in the terminal
When was Node.js created?
2009
What back end languages have you heard of?
Node.js, PHP
What is a computer process?
An instance of a computer program being executed on one or more threads
Roughly how many computer processes are running on your host operating system (Task Manager or Activity Monitor)?
120
Why should a full stack Web developer know that computer processes exist?
It’s important to know what processes are running at any given time so you can work with them
What is the process object in a Node.js program?
The process object is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require().
How do you access the process object in a Node.js program?
process
What is the data type of process.argv in Node.js?
array
What is a JavaScript module?
A single .js file
What values are passed into a Node.js module’s local scope?
__dirname, __filename, exports, module, require
Give two examples of truly global variables in a Node.js program.
global, process
What is the purpose of module.exports in a Node.js module?
To transfer information between JavaScript files
How do you import functionality into a Node.js module from another Node.js module?
Assign whatever you want to be exported to module.export; Then use require(“./fileName”) in the file where you want to import that information
What is the JavaScript Event Loop?
The interaction between the call stack and the callback queue, which checks if the queue is empty or not, and if something is in the queue, it gets pushed to the call stack
What is different between “blocking” and “non-blocking” with respect to how code is executed?
Blocking refers to code that is running, which blocks other code from being executed on the webpage until that stack is finished
What is a directory?
A file system for storing data on your computer
What is a relative file path?
A location that is relative to the current directory
What is an absolute file path?
The complete location of the file, including which drive it was on
What module does Node.js include for manipulating the file system?
fs
What method is available in the Node.js fs module for writing data to a file?
writeFile
Are file operations using the fs module synchronous or asynchronous?
asynchronous
What is a client?
A piece of computer hardware or software that interacts with a server.
What is a server?
A piece of computer hardware or software that provides functionality for clients.
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
What is on the first line of an HTTP request message?
An HTTP method, request target, and the HTTP version
What is on the first line of an HTTP response message?
The protocol version, a status code, a status text
What are HTTP headers?
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value.
Is a body required for a valid HTTP message?
No
What is AJAX?
Ajax allows you to update parts of the DOM of an HTML page instead without the need for a full page refresh. Ajax also lets you work asynchronously, meaning your code continues to run while the targeted part of your web page is trying to reload (compared to synchronously, which blocks your code from running until that part of your page is done reloading).
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
What is NPM?
A software registry that allows users to share and borrow packages
What is a package?
Reusable code from other users that uploaded them to the npm website
How can you create a package.json with npm?
You navigate to the directory and then run
npm init –yes
What is a dependency and how to you add one to a package?
The packages installed by the user and required for the project
npm install package name
What happens when you add a dependency to a package with npm?
The package is added to a node_modules folder
How do you add express to your package dependencies?
npm install express
What Express application method starts the server and binds it to a network PORT?
listen()
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
What is the significance of an HTTP request’s method?
It indicates the desired action for the server to take
What does the express.json() middleware do and when would you need it?
It parses incoming requests with JSON payloads; Whenever you use a post request, since you are sending data to the server
What is Array.prototype.filter useful for?
It is useful for creating a new array when you only want to grab specific values from an existing array that meet a specific condition
What is Array.prototype.map useful for?
when you want to run a function on every index in the array and return the same amount of values as the input array
What is Array.prototype.reduce useful for?
When you want to run a function on every index in the array and return a single value
What is “syntactic sugar”?
syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express.
What is the typeof an ES6 class?
Function
Describe ES6 class syntax.
You define a class, and inside that class you define a constructor function where you pass the parameters you’re expecting, and then you can define multiple methods that you would want to run on that class
What is “refactoring”?
Rewriting code to make it more readable, while still maintaining the same functionality
What is Webpack?
A module bundler
How do you add a devDependency to a package?
npm install –save-dev
What is an NPM script?
an npm command that you define, that when called runs whatever you assigned to the script
How do you execute Webpack with npm run?
npm run scriptName
How are ES Modules different from CommonJS modules?
Their syntax is even more compact than CommonJS’s.
Their structure can be statically analyzed (for static checking, optimization, etc.).
Their support for cyclic dependencies is better than CommonJS’s.
What kind of modules can Webpack support?
ES, CommonJS, AMD
What is React?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces.
What is a React element?
Plain objects you create where you specific the element and value, that can be rendered into the DOM
How do you mount a React element to the DOM?
ReactDOM.render(element, container[, callback])
What is Babel?
babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. It also converts JSX
What is a Plug-in?
a software component that adds a specific feature to an existing computer program.
What is a Webpack loader?
loaders allow you to specify which files you want a specific dependency to run on before webpack compiles your files
How can you make Babel and Webpack work together?
You configure webpack to specify which files you want Babel to transpile before webpack compiles all your files
What is JSX?
A nice and readable way of creating elements in React
Why must the React object be imported when authoring JSX in a module?
You need to import react and react-dom in order to create DOM elements in React
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
Using a babel-loader along with a babel plugin to convert JSX
What is a React component?
Pieces of reusable code
How do you define a function component in React?
defining a function or a class; function Welcome() { return <h1>Hello</h1>; }; class Welcome extends React.Component { render() { return <h1>Hello</h1>; } }
How do you mount a component to the DOM?
ReactDOM.render(
,
document.getElementById(‘root’)
);
What are props in React?
objects which are used to pass data between components
How do you pass props to a component?
props as a parameter in your function/class
How do you write JavaScript expressions in JSX?
in curly braces
How do you create “class” component in React?
class ClassName extends React.Component
How do you access props in a class component?
this
What is the purpose of state in React?
to control what you want the user to see on the webpage
How to you pass an event handler to a React element?
onClick attribute or different event attributes
What Array method is commonly used to create a list of React elements?
map
What is the best value to use as a “key” prop when rendering lists?
Ids or a unique string
What are controlled components?
An input form element whose value is controlled by state
What two props must you pass to an input for it to be “controlled”?
handle submit & handle change
What does express.static() return?
a function
What is the local __dirname variable in a Node.js module?
the directory name of the current module
What does the join() method of Node’s path module do?
It combines all its arguments to make a path
What does fetch() return?
a promise
What is the default request method used by fetch()?
get
How do you specify the request method (GET, POST, etc.) when calling fetch?
you define the desired method inside of the init object within your fetch call
When does React call a component’s componentDidMount method?
After a component was rendered
Name three React.Component lifecycle methods.
componentWillUnmount componentDidUpdate, componentDidMount
How do you pass data to a child component?
props
What must the return value of myFunction be if the following expression is possible?
myFunction()();
another function
What does this code do? const wrap = value => () => value;
A function that returns another function which returns the parameter from the 1st function
What allows JavaScript functions to “remember” values from their surroundings?
closures