JavaScript Flashcards
What is the purpose of variables?
to store data and information to be used later. keeps a history of data
How do you declare a variable?
var
let
const
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
letters, $, _, numbers(numbers cannot be the first character)
What does it mean to say that variable names are “case sensitive”?
casing matters in variables
What is the purpose of a string?
to be able to give a variable a value of text
What is the purpose of a number?
to assign variables a numerical value
What is the purpose of a boolean?
making decisions
What does the = operator mean in JavaScript?
assigns a value to something so that it will contain it
How do you update the value of a variable?
declare and assign it a new value
What is the difference between null and undefined?
null is an empty value purposely given while undefined means nothing is defined yet
Why is it a good habit to include “labels” when you log values to the browser console?
specifies what the value relates to
Give five examples of JavaScript primitives.
boolean string number null undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
appending a string to another end of a string
What purpose(s) does the + plus operator serve in JavaScript?
string concatenation and addition
What data type is returned by comparing two values (< , > , ===, etc)?
boolean
What does the += “plus-equals” operator do?
addition assignment and the result of that will be the updated value
What are objects used for?
to store multiple types of data
What are object properties?
a piece of data stored in an object
Describe object literal notation.
data within a { and a , separating each one
How do you remove a property from an object?
delete object.property
What are the two ways to get or update the value of a property?
dot notations and square brackets
What are arrays used for?
objects with similar data types in a list format
Describe array literal notation.
declare a variable
square bracket
values contained in the bracket
How are arrays different from “plain” objects?
arrays are ordered, indexed numerically, constant count of what is inside
What number represents the first index of an array?
0
What is the length property of an array?
number of values in the array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
reusable block of code
Describe the parts of a function definition.
function keyword name of function parameter(s) code block return keyword
Describe the parts of a function call.
( )
When comparing them side-by-side, what are the differences between a function call and a function definition?
function calls does not have the keyword function function calls have arguments and data
What is the difference between a parameter and an argument?
parameter is the place holder which is used when defining a function
arguments replace the parameter when the function is being called
Why are function parameters useful?
so that you can provide values with arguments when the function is being called
What two effects does a return statement have on the behavior of a function?
produces a value from the function
it stops the code of the function from running any further
Why do we log things to the console?
verify outputs. a debugging mechanism
What is a method?
function stored in a property
How is a method different from any other function?
methods are attached to objects
How do you remove the last element from an array?
pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
gives a number from 0-1. inclusive of 0 but not of 1.
How do you delete an element from an array?
splice()
How do you append an element to an array?
push()
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
console.log() to check
Is the return value of a function or method useful in every situation?
No. You may be using the function or method for what it does and not necessarily the return
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?
booleans
What is the purpose of an if statement?
to determine which code block is to be run
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, conditional code block
if (condition) {
code block
}
What are the three logical operators?
&& , || , !
How do you compare two different expressions in the same condition?
&& , ||
Why do we log things to the console?
to make sure our code is correct
What is a “model”?
something that is easier to represent of a concept.
a representation of something
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?
referring to javascript objects
What is a DOM Tree?
collection of html elements similar to a family tree
Give two examples of document methods that retrieve a single element from the DOM.
querySelectorAll
querySelector
(use these methods only)
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 data and use it later
What console method allows you to inspect the properties of a DOM element object?
dir method
dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
because it is read top to bottom. if you put the script tag above anything, it won’t be read
What does document.querySelector() take as its argument and what does it return?
css selector as an argument in a string form.
returns one object
finds the first selector and disregards the rest
What does document.querySelectorAll() take as its argument and what does it return?
css selector as an argument in a string form.
returns an object
finds all selectors and produces it
Why do we log things to the console?
to check if your code works
What is the purpose of events and event handling?
is for user interaction
Are all possible parameters required to use a JavaScript method or function?
no
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function?
a function that is passed inside another function as an argument
What object is passed into an event listener callback when the event fires?
it is the object javascript created when the event happens which is passed into the call back function
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
object referencing where ever this event started from
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
first one has a callback function
second one has a function call (second one, the event will happen immediately when the page loads. event listener will not work)
What is the className property of element objects?
sets the name of a class name
How do you update the CSS class attribute of an element using JavaScript?
object.className = “new class name”
What is the textContent property of element objects?
allows you to update the text of the object
How do you update the text within an element using JavaScript?
.textContent on a dom object with an assignment operator to update the text
object.textContent = “text content”
Is the event parameter of an event listener callback always useful?
no
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
its more easily accessible in a variable
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 form?
submit
What does the event.preventDefault() method do?
it stops the default behavior of something
example: putting event.preventDefault() within a function in an event listener with submit buttons will make sure the page does not refresh when you click submit
What does submitting a form without event.preventDefault() do?
it will refresh the page
What property of a form element object contains all of the form’s controls.
.elements
What property of a form 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?
it might not work
and you don’t know what is not working
What is an advantage of having your console open when writing a JavaScript program?
to see if things are working or aren’t working
Does the document.createElement() method insert a new element into the page?
no because it has not been added visible portion yet
How do you add an element as a child to another element?
parent.appendChild(child)
What do you pass as the arguments to the element.setAttribute() method?
the string of the name of the attribute
second is the string of the value of the attribute
What steps do you need to take in order to insert a new element into the page?
called the createElement method and assign it to a variable
appendChild this new element by passing the variable as the argument
What is the textContent property of an element object for?
set the text content of a specific element
Name two ways to set the class attribute of a DOM element.
setAttribute and className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you do not have to write the code over again when you have a function
you can make it loop
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you can call the function multiple times instead of just one use
What is the event.target?
the element where the event started from
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What DOM element property tells you what type of element it is?
tagname property
What does the element.closest() method take as its argument and what does it return?
css selector format as a string
return is a closest ancestor that matches that selector
How can you remove an element from the DOM?
remove method
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?
event delegation
What is the event.target?
element where the event started
What is the affect of setting an element to display: none?
the element cannot be seen or interacted with
What does the element.matches() method take as an argument and what does it return?
the argument is the name of the element as a string in css format
the return a true or false statement (boolean)
How can you retrieve the value of an element’s attribute?
element.getAttribute()
At what steps of the solution would it be helpful to log things to the console?
every step
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?
you would have to write another function in javascript for this tab
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?
you would need another function to show and hide each page
What is a breakpoint in responsive Web design?
Its the point where the styling changes
What is the advantage of using a percentage (e.g. 50%) width instead of a fixed (e.g. px) width for a “column” class in a responsive layout?
a percentage width will be responsive and adaptive to the viewport
If you introduce CSS rules for a smaller min-width after the styles for a larger min-width in your style sheet, the CSS rules for the smaller min-width will “win”. Why is that?
Source order lets the smaller min-width take priority
What is JSON?
javascript object notation
text format of an object
this will be in a string data type
What are serialization and deserialization?
serialization - object into string
deserialization - string into object
Why are serialization and deserialization useful?
its useful transferring data
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
How to you store data in localStorage?
localStorage.setItem()
How to you retrieve data from localStorage?
localStorage.getItem()
What data type can localStorage save in the browser?
strings
When does the ‘beforeunload’ event fire on the window object?
before you leave the page
What is a method?
a function stored in the property of an object
How can you tell the difference between a method definition and a method call?
method definition is in the property value. has a function keyword, and a code block
method call is used with dot notation and has a parenthesis
object.method()
Describe method definition syntax (structure).
function keyword, parameters, and a colon for assigning it to a property, property name
Describe method call syntax (structure).
object.method()
How is a method different from any other function?
methods are within 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
You usually flip a light switch on or off to control the light in a room. The reality is that you are closing an electrical circuit that connects to a mains breaker somewhere in your home so that 120 volts at 15 amps goes through a light bulb. But to you, you are “turning on the lights”.
What does API stand for?
Application Programming Interface
a set of tools someone created to let you interact with
What is the purpose of an API?
communications between multiple software
What is this in JavaScript?
The object you are working with
What does it mean to say that this is an “implicit parameter”?
an implicit parameter is not named
When is the value of this determined in a function; call time or definition time?
call time
How can you tell what the value of this will be for a particular function or method definition?
it is impossible to find out what is this if the function is only being defined
How can you tell what the value of this is for a particular function or method call?
just the object to the left of the dot
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance
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?
prototypes
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
to its prototype
What does the new operator do?
Creates a blank, plain JavaScript object.
Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
Returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
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?
function passed into another function as an argument or value
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?
setTime()
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?
time out id
What is AJAX?
a group of technologies that offer asynchronous functionality in the browser
application designed to retrieve data and put it on a portion of a webpage without refreshing the whole page
What does the AJAX acronym stand for?
it stood for Asynchronous Javascript and XML, but the technologies have moved on, but the term Ajax is still used
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequests
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
“load”
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
because it is an object that supports events
What is the purpose of a loop?
to keep executing a block of code until the condition is false
What is the purpose of a condition expression in a loop?
when a loop will end
What does “iteration” mean in the context of loops?
one loop
When does the condition expression of a while loop get evaluated?
before the code block is executed
When does the initialization expression of a for loop get evaluated?
before the loop begins
When does the condition expression of a for loop get evaluated?
evaluated before the code block is run every time
When does the final expression of a for loop get evaluated?
right after each code block is executed
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?
increment a value by 1
How do you iterate through the keys of an object?
for in loop
What is a code block? What are some examples of a code block?
code within {}
What does block scope mean?
relevant to that specific code block. it is no longer accessible outside of that scope
What is the scope of a variable declared with const or let?
blocked scoped
What is the difference between let and const?
the variable of const cannot be reassigned using the assignment operator. it can be changed and mutated, but not reassigned
Why is it possible to .push() a new value into a const variable that points to an Array?
because const is storing the reference of the array. you can still change the array but you cannot reassign const using the assignment operator again
How should you decide on which type of declaration to use?
if the variable is going to be reassigned, use let
if the variable is going to stay const, use const
What is the syntax for writing a template literal?
`` for strings
${ } for variables
What is “string interpolation”?
The JavaScript engine will automatically replace these variables and expressions with their values when using template literals. This feature is known as string interpolation.
What is destructuring, conceptually?
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
taking objects and arrays and converting it into a smaller object, arrays, and even variables
What is the syntax for Object destructuring?
var {name of property} = object
What is the syntax for Array destructuring?
var [name of property] = array
How can you tell the difference between destructuring and creating Object/Array literals?
object and array literals have their brackets on the right side
if the brackets are on the left side, it is destructuring
What is the syntax for defining an arrow function?
parameter and the arrow symbol
parenthesis is required only if there are no parameters or more than one
When an arrow function’s body is left without curly braces, what changes in its functionality?
if there is a curly brace, you need to have a return statement.
if there is no curly brace, the return statement is implied
How is the value of this determined within an arrow function?
where the function is defined
What is a CLI?
command-line interface
What is a GUI?
graphical user interface
Give at least one use case for each of the commands listed in this exercise. man cat ls pwd echo touch mkdir mv rm cp
man - used to view the manuals of a command
cat - used to concatenate files and print on the standard output
ls - list directory contents
pwd- print name of current/working directory
echo- display a line of text
touch- change file timestamps
mkdir- make directories
mv- move (rename) files
rm- remove files or directories
cp- copy files and directories
What are the three virtues of a great programmer?
- Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don’t have to answer so many questions about it.
- Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don’t just react to your needs, but actually anticipate them. Or at least pretend to.
- Hubris: The quality that makes you write (and maintain) programs that other people won’t want to say bad things about.
What is the JavaScript Event Loop?
it is a function within the browser that pushes events that were previously delayed (such as setTimeout or SetInterval) into action by pushing it into the “stack”. It only pushes it into the “stack” the “stack” is empty.
it is how asynchronous callbacks are called when the stack is clear
What is different between “blocking” and “non-blocking” with respect to how code is executed?
if something is “blocking” the event loop cannot occur as code still needs to be executed before it is unblocked
What is different between “blocking” and “non-blocking” with respect to how code is executed?
if something is “blocking” (something is in the stack) the event loop cannot occur as code still needs to be executed before it is unblocked
What is a directory?
a directory is a way of organizing data. directories contain other files and possibly other directories
What is a relative file path?
a file path starting from the current directory
What is an absolute file path?
it is a file path to any file anywhere in the system starting from the root
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?
depending on the method, they are ether synchronous or asynchronous
What is NPM?
makes it easier for javascript developers to share the code they have created to solve particular problems
stands for node package manager
npm consists of three distinct components:
the website
the Command Line Interface (CLI)
the registry
What is a package?
a package is a directory with one or more files in it.
in terms of NPM, the package will include package.json that will include some meta data
How can you create a package.json with npm?
npm init –yes
What is a dependency and how to you add one to a package?
a package you download to run your program. the package should generally have code to implement in your own code
npm install jquery
What happens when you add a dependency to a package with npm?
will add a node_modules folder and add the package into there. it will also update the dependencies property of package.json
What are the three states a Promise can be in?
pending
fulfilled
rejected
How do you handle the fulfillment of a Promise?
using .then()
the first argument of a then() should be the function that handles the fulfillment
How do you handle the rejection of a Promise?
using .then() or .catch()
the second argument of then() should be the function that handles the rejection
What is Array.prototype.filter useful for?
useful for filtering out elements in arrays to a new array
What is Array.prototype.map useful for?
Creating a new array containing the transformed elements of another.
What is Array.prototype.reduce useful for?
combined everything into one value.
output is an array
What is “syntactic sugar”?
a style of code used to make it easier to read and write
What is the typeof an ES6 class?
function
Describe ES6 class syntax
class Person { constructor(name) { this.name = name; } getName() { return this.name; } } constructor method is optional.
What is “refactoring”?
the process of restructuring to make code better but the functionality is the same
How are ES Modules different from CommonJS modules?
ES Modules are static and have to go to the top of the file (will be using this for front-end in class)
CommonJS is dynamic and do not have to be at the top of the file (will be using this for back-end in class)
What kind of modules can Webpack support?
ECMA script modules, CommonJS modules, AMD, web assembly
What does fetch() return?
returns a promise to resolve the response object
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
by passing it an object as another argument in fetch() and have a property key method, followed by which ever request method you want in a string form
{
method: “GET”
}
What must the return value of myFunction be if the following expression is possible? myFunction()();
the return of the inner function
What does this code do? const wrap = value => () => value;
const wrap = function (value) { return function (){ return value }
the variable wrap is holding the function definition. if wrap is called with an argument, it returns a second function with no parameters. If the second function is called, it returns the first argument.
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
when its defined
What allows JavaScript functions to “remember” values from their surroundings?
closures